我正试图将我的尾巴从parser
转发到add_option
,但GCC不允许这样做:
// Templated constructor
template <typename H = std::string, typename... T>
parser(H head, T... tail) {
/* Add an option */
add_option(std::forward<T>(tail)...);
}
// Base case
void add_option(std::string head) {
/* Add an option */
}
// Recursive case
template <typename H = std::string, typename... T>
void add_option(H head, T... tail) {
add_option(tail...);
}
相反,GCC给了我这个错误:
parser.h: In instantiation of ‘void parser::add_option(H, T ...) [with H = const char*; T = {}]’:
parser.h:16:4: required from ‘parser::parser(H, T ...) [with H = const char*; T = {const char*}]’
parser.cpp:7:26: required from here
parser.h:24:4: error: no matching function for call to ‘parser::add_option()’
add_option(tail...);
^
parser.h:24:4: note: candidates are:
parser.h:18:8: note: void parser::add_option(std::string)
void add_option(std::string head) {
^
parser.h:18:8: note: c1andidate expects 1 argument, 0 provided
parser.h:22:8: note: template<class H, class ... T> void parser::add_option(H, T ...)
void add_option(H head, T... tail) {
^
parser.h:22:8: note: template argument deduction/substitution failed:
parser.h:24:4: note: candidate expects 2 arguments, 0 provided
add_option(tail...);
^
Makefile:2: recipe for target 'parser' failed
make: *** [parser] Error 1
有谁能告诉我这里我做错了什么?我该如何解决这个问题?
答案 0 :(得分:1)
struct parser {
template <typename... Ts>
parser(std::string head, Ts... ts) {
add_options(std::move(head), std::forward<Ts>(ts)...);
}
// Base case
void add_options(std::string head) {
// I assume we store head somewhere. `std::move` from it
// if we don't directly store it, replace all std::string with std::string const&
std::cout << "option: " << head.c_str() << "\n";
}
// Recursive case. Make 2+ arguments explicit (probably not needed, but I like it)
template <typename T0, typename... Ts>
void add_options(std::string head, T0&& t0, Ts&&... ts) {
add_options(std::move(head));
add_options(std::forward<T0>(t0), std::forward<Ts>(ts)...);
}
};
int main() {
parser parse("hello", "world");
}