鍏舵枃妗d緥瀛愪腑鏈夊涓嬩唬鐮侊細(xì)
1 using namespace boost::program_options;
2 //澹版槑闇瑕佺殑閫夐」
3 options_description desc("Allowed options");
4 desc.add_options()
5 ("help,h", "produce help message")
6 ("person,p", value<string>()->default_value("world"), "who");
鐪嬬4鍒?琛岋紝鏄笉鏄劅瑙夊緢鎬紵榪欑鏂瑰紡浣撶幇浜?jiǎn)鍑芥暟寮従~栫▼涓渶澶х殑鐗圭偣錛氬嚱鏁版槸涓綾誨鹼紝寮曠敤璧勬枡鏉ヨ,鎵璋?#8220;鍑芥暟鏄竴綾誨鹼紙F(tuán)irst Class Value錛?#8221;鎸囩殑鏄嚱鏁板拰鍊兼槸鍚岀瓑鐨勬蹇碉紝涓涓嚱鏁板彲浠ヤ綔涓哄彟澶栦竴涓嚱鏁扮殑鍙傛暟錛屼篃鍙互浣滀負(fù)鍊間嬌鐢ㄣ傚鏋滃嚱鏁板彲浠ヤ綔涓轟竴綾誨間嬌鐢紝閭d箞鎴戜滑灝卞彲浠ュ啓鍑轟竴浜涘嚱鏁幫紝浣垮緱榪欎簺鍑芥暟鎺ュ彈鍏跺畠鍑芥暟浣滀負(fù)鍙傛暟騫惰繑鍥炲彟澶栦竴涓嚱鏁般傛瘮濡傚畾涔変簡(jiǎn)f鍜実涓や釜鍑芥暟錛岀敤compose(f,g)鐨勯鏍煎氨鍙互鐢熸垚鍙﹀涓涓嚱鏁幫紝浣垮緱榪欎釜鍑芥暟鎵цf(g(x))鐨勬搷浣滐紝鍒欏彲縐癱ompose涓洪珮闃跺嚱鏁幫紙Higher-order Function錛夈?br>
program_options閲岀殑榪欑鏂瑰紡鏄庝箞瀹炵幇鐨勫憿錛熼氳繃鍒嗘瀽boost鐨勬簮浠g爜錛屾垜浠嚜宸辨潵鍐欎釜綾諱技鐨勫疄鐜扮湅鐪嬶細(xì)
test.h
1 #pragma once
2
3 #include <iostream>
4 using namespace std;
5
6 class Test;
7
8 class Test_easy_init
9 {
10 public:
11 Test_easy_init(Test* owner):m_owner(owner){}
12
13 Test_easy_init & operator () (const char* name);
14 Test_easy_init & operator () (const char* name,int id);
15 private:
16 Test* m_owner;
17 };
18
19
20 class Test
21 {
22 public:
23 void add(const char* name);
24 void add(const char* name,int id);
25
26 Test_easy_init add_some();
27
28 };
test.cpp
1 #include "test.h"
2
3 Test_easy_init & Test_easy_init::operator () (const char* name,int id)
4 {
5
6 m_owner->add(name,id);
7 return *this;
8 }
9
10
11 Test_easy_init & Test_easy_init::operator () (const char* name)
12 {
13
14 m_owner->add(name);
15 return *this;
16 }
17
18 Test_easy_init Test::add_some()
19 {
20 return Test_easy_init(this);
21 }
22
23
24 void Test::add(const char* name)
25 {
26 cout<<"add:"<<name<<endl;
27 }
28
29 void Test::add(const char* name,int id)
30 {
31 cout<<"add:"<<name<<"-"<<id<<endl;
32 }
浣跨敤鏂瑰紡錛?br>
1 Test t1;
2
3 t1.add_some()
4 ("hello",1)
5 ("no id")
6 ("hello2",2);
鏄笉鏄緢鏈夋剰鎬濄俛dd_some()鏂規(guī)硶榪斿洖涓涓猅est_easy_init綾葷殑瀵硅薄錛孴est_easy_init綾婚噸杞戒簡(jiǎn)鎿嶄綔絎?)錛屾搷浣滅()鏂規(guī)硶榪斿洖Test_easy_init綾誨璞¤嚜韜殑寮曠敤銆傘?

]]>