25

Boost の Program Options ライブラリを使用するプログラムを書いています。

desc.add_options()
        ("help","produce help message")
        ( /* other flag, value, description pairs here */)
;

ヘッダーで operator() がオーバーライドされていることがわかりますが、これを構文的に正しくする方法がわかりません。

第二に、 add_options() を複数回呼び出すのと比較して、この構文には利点がありますか (このような構文を操作できるという事実を誇示することに加えて)?

4

2 に答える 2

23

メンバー関数は、タイプのadd_optionsオブジェクトを返しますoptions_description_easy_init。後者はoperator()、自身への参照を返すためにオーバーロードされています。これにより、スニペットで示したように呼び出しを連鎖させることができます。

呼び出しの連鎖と複数回の呼び出しの違いはadd_options、前者の場合、 の単一のインスタンスoptions_description_easy_initが作成され、それを呼び出すたびoperator()に、所有者 ( ) にオプションが追加されることoptions_descriptionです。add_options複数回呼び出すと、各呼び出しで の新しいインスタンスが作成されますoptions_description_easy_init

于 2012-05-07T17:57:07.347 に答える
14

利点の質問は主観的ですが、この場合は簡潔です。

私のホームプロジェクトの1つからこれを比較してください:

("help,h", "Generate this help message")
("output-file,o", po::value<std::string>(), "Output filename. Required.")
("tangent,t", "Generate/load tangent-space basis.")
("collada-output,c", "Write a Collada file, rather than our mesh XML format.")
("arrays,a", "Write arrays instead of indexed verts. Cannot combine with Collada writing.")
("flip-tangent,f", "Change the tangent-space basis matrix's handedness. Negates bitangent.")
("map", po::value<std::string>(), "Map filename. Defaults to the ColladaConv directory's 'stdmap.txt' file.")
("vao", po::value<std::vector<std::string> >(), "Sequence of mappings, of the form:\n"
        "Name # # # #\n"
        "\n"
        "Each # is an attribute index to use for this VAO.\n"
        "Each VAO name must be unique; you cannot use the same VAO in the same place.")

これに:

visible.add_options()("help,h", "Generate this help message")
visible.add_options()("output-file,o", po::value<std::string>(), "Output filename. Required.")
visible.add_options()("tangent,t", "Generate/load tangent-space basis.");
visible.add_options()("collada-output,c", "Write a Collada file, rather than our mesh XML format.");
visible.add_options()("arrays,a", "Write arrays instead of indexed verts. Cannot combine with Collada writing.");
visible.add_options()("flip-tangent,f", "Change the tangent-space basis matrix's handedness. Negates bitangent.");
visible.add_options()("map", po::value<std::string>(), "Map filename. Defaults to the ColladaConv directory's 'stdmap.txt' file.");
visible.add_options()("vao", po::value<std::vector<std::string> >(), "Sequence of mappings, of the form:\n"
        "Name # # # #\n"
        "\n"
        "Each # is an attribute index to use for this VAO.\n"
        "Each VAO name must be unique; you cannot use the same VAO in the same place.");

行の長さが重要です。そしてvisible.add_options()、すべての前にいる必要がないので、読みやすくなります。

于 2012-05-07T19:09:47.440 に答える