template <size_t size, typename ...Params>
void doStuff(Params...) {
}
template <>
void doStuff<size_t(1), int, bool>(int, bool) {
}
int main(int, char**) {
doStuff<1,int,bool>(1, false);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不会编译,第二个doStuff声明给了我,error: template-id ‘doStuff<1u, int, bool>’ for ‘void doStuff(int, bool)’ does not match any template declaration但它明确地将第一个声明与variadic模板参数匹配.
如何专门研究可变参数模板?
c++ templates template-specialization variadic-templates c++11
使用旧版本的g ++(4.8.0,MinGW)编译项目我发现此代码无法编译:
template<typename T>
void foo() = delete;
template<>
void foo<int>(){}
int main() {
foo<int>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
似乎g ++甚至没有尝试寻找显式特化,如果它看到基本情况被删除.
mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ -std=c++11 buggy_deleted_template.cpp
buggy_deleted_template.cpp: In function 'int main()':
buggy_deleted_template.cpp:8:14: error: use of deleted function 'void foo() [with T = int]'
foo<int>();
^
buggy_deleted_template.cpp:5:6: error: declared here
void foo<int>(){}
^
mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ --version
i686-w64-mingw32-g++ (rubenvb-4.8.0) 4.8.0
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for …Run Code Online (Sandbox Code Playgroud) c++ templates template-specialization deleted-functions c++11