我正在尝试将我的尾巴从 to 转发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
谁能告诉我我在这里做错了什么?我怎样才能解决这个问题?