2016-12-07 3 views
0

私はブーストを使用しています:: program_optionsライブラリと以下のコードは、オプションの説明を作成し、それにオプションを追加するために使用されます。関数呼び出し後にカッコを繰り返すC++言語機能はありますか?

po::options_description opts("SendFile Options"); 

    opts.add_options()("help,h", "Print this help message") 
     ("other,o","This is the other"); 

私の質問は、あなたが追加することを可能にするC++言語の機能であり、 add_options関数を括弧で囲まれた繰り返し値の形で呼び出した直後に別の記述を使用するか?これは何と呼ばれ、このように機能する関数をどのように作成するのですか?

+2

ちょうど呼び出し可能なものを返す関数を記述します。 – Biffen

+1

'operator()'をオーバーロードすることができます – wally

+0

非常に近いhttps://stackoverflow.com/questions/10486588/boost-program-options-add-options-syntax –

答えて

2

簡体例:

#include <string> 
#include <vector> 
#include <iostream> 

class Options { 
public: 
    Options& operator()(std::string text) 
    { 
     strings.push_back(text); 
     return *this; 

    } 
    std::vector<std::string> strings; 

}; 

int main() 
{ 
    Options options{}; 
    options("Some text") 
     ("more text") 
     ("even more text"); 
    for(const auto& text : options.strings) 
     std::cout << text << '\n'; 
} 

が生成されます

Some text 
more text 
even more text 
関連する問題