【发布时间】:2018-09-04 10:20:00
【问题描述】:
我知道函数可以是模板参数。但是 GCC、Clang 和 MSVC(编译版本在rextester)在模板为可变参数时不编译编译,如下所示:
void Func( int ){}
template<void (*)(int)>
struct Foo{};
template struct Foo<Func>; // Compiles
template<typename>
struct Bar;
template<typename ...Args>
struct Bar<void(*)(Args...)>
{
};
template struct Bar<Func>; // Does NOT compile (why???)
int main()
{
}
MSVC 产生最详细的输出和可能的解释(正确或错误),说明代码为何无法编译。
source_file.cpp(20): error C2923: 'Bar': 'Func' is not a valid template type argument for parameter 'T'
source_file.cpp(1): note: see declaration of 'Func'
source_file.cpp(20): error C2990: 'Bar': non-class template has already been declared as a class template
source_file.cpp(13): note: see declaration of 'Bar'
source_file.cpp(20): error C2946: explicit instantiation; 'Bar' is not a template-class specialization
什么是传递函数的适当语法,这些函数本身接受任意数量的参数作为类模板参数。
【问题讨论】: