2

我正在尝试编写这样的模板。

template <char c,typename Options, 
   basic_string<class,class,class> Options::* member> 
   struct option_string;

但是我不断收到编译错误。我想知道如何编写一个包含三个选项的模板:a char、任何类和指向该类成员的指针,该成员必须是basic_string特化的。我想避免为basic_string.

我的目标是创建一个通用的命令行选项库。以下是我想如何使用它。

class Options : public Command_Line_Options_Parser<Options> {
public:
  int integer = 0;
  float real_number = 0.0;
  bool boolean = false;
  bool make_true = false;
  bool make_false = true;
  std::string string;
  typedef Options O;
  typedef Command_Line_Options<
            option_int<'i',&O::integer>,
            option_float<'f',&O::real_number>,
            option_bool<'b',&O::boolean>,
            option_true<'t',&O::make_true>,
            option_false<'t',&O::make_true>,
            option_string<'s',&O::string>
           >
  option_list;
};

int main(int argc,char**argv ) {

  Options options;

  options.parse(argc,argv);
  std::cout << "integer : " << options.integer <<endl;
  std::cout << "real_number : " << options.real_number <<endl;
  std::cout << "boolean : " << options.boolean <<endl;
  std::cout << "make_true : " << options.make_true <<endl;
  std::cout << "make_false : " << options.make_false <<endl;
  std::cout << "string : " << options.string <<endl;
};

我宁愿option_string接受所有专业basic_string而不是仅仅接受std::string.

4

1 回答 1

6

您不必直接指定返回类型。在那种情况下,为什么要basic_string直接在课堂上打扰呢?只需采取typename String并完成:

template<char C, class Options, class String, String Options::* Member>
struct option_string;

如果您仍然想确保它String确实是 的专业化basic_string,那么,请使用这个小助手和static_assert它:

#include <type_traits>
#include <string>

template<class T>
struct is_basic_string : std::false_type{};

template<class Ch, class Tr, class Al>
struct is_basic_string<std::basic_string<Ch,Tr,Al>> : std::true_type{};

// in 'option_string'
static_assert(is_basic_string<String>::value,
    "Data member must be a 'basic_string' specialization.");

由于似乎不需要将成员指针作为模板参数传递,我建议只在构造函数中传递它:

template<char C, class Option, class String>
struct option_string{
  static_assert(is_basic_string<String>::value,
      "Data member must be a 'basic_string' specialization.");
  typedef String Option::*member_type;

  option_string(member_type m, ...) : member(m), ... { ... }
private:
  member_type member;
};

template<char C, class Option, class String>
option_string<C, Option, String> make_option_string(String Option::*member, ...){
  return {member, ...};
}

// in code:
auto os = make_option_string<'H'>(&some_type::a_string_member);
于 2012-09-24T19:05:45.413 回答