C++ 将函数f传递给以f类型为模板的类的构造函数
我正在探索两种不同的方法来将函数传递给类模板的ctorC++ 将函数f传递给以f类型为模板的类的构造函数,c++,templates,specialization,variadic,C++,Templates,Specialization,Variadic,我正在探索两种不同的方法来将函数传递给类模板的ctorBar,如下和所示 测试的编译器:GCC 5.4.0和clang 3.8.0。(在线版本中禁用了有问题的构造。) 问题2 我第二次尝试将函数传递到Bar时,使用函数void wrapper(F)调用Bar(F)。奇怪的是,两个编译器都使用了Bar的(未实现的)基本模板,而不是产生错误的专门化 source_file.cpp:37:9: error: implicit instantiation of undefined template 'B
Bar
,如下和所示
测试的编译器:GCC 5.4.0和clang 3.8.0。(在线版本中禁用了有问题的构造。)
问题2
我第二次尝试将函数传递到Bar
时,使用函数void wrapper(F)
调用Bar(F)
。奇怪的是,两个编译器都使用了Bar
的(未实现的)基本模板,而不是产生错误的专门化
source_file.cpp:37:9: error: implicit instantiation of undefined template 'Bar<void (*)(bool, short, int)>'
Bar<F> bar( f );
^
source_file.cpp:42:2: note: in instantiation of function template specialization 'wrapper<void (*)(bool, short, int)>' requested here
wrapper( wiggle );
^
source_file.cpp:13:8: note: template is declared here
struct Bar;
source_file.cpp:37:9:错误:未定义模板“Bar”的隐式实例化
巴(f);
^
source_file.cpp:42:2:注意:在函数模板专门化的实例化中,此处请求“包装器”
包装(摆动);
^
source_file.cpp:13:8:注意:此处声明了模板
结构杆;
特别令人困惑的是,type name info dump macro
DEMANGLE
确认传递给wrapper
的F
类型是void(*)(bool,short,int)
问题是wiggle
在传递给wrapper
时隐式转换为函数指针,因此,F
的实际类型是:
void (*)(bool, short, int)
…无法匹配void(Args)
。您可以使用:
…这一点(在我看来)更清楚。您想在构造函数中使用
func
做什么?之后?从你的问题中删除DEMANGLE内容。它只是把东西弄得乱七八糟。另外,霍尔特的回答是正确的。@Holt我不想谈这些。它将添加更多不必要的信息。就说我想调用func
。谢谢你的回答。
source_file.cpp:37:9: error: implicit instantiation of undefined template 'Bar<void (*)(bool, short, int)>'
Bar<F> bar( f );
^
source_file.cpp:42:2: note: in instantiation of function template specialization 'wrapper<void (*)(bool, short, int)>' requested here
wrapper( wiggle );
^
source_file.cpp:13:8: note: template is declared here
struct Bar;
void (*)(bool, short, int)
Bar<std::remove_pointer_t<F>> bar( f );
Bar(void (*func)(Args...))