【发布时间】:2019-07-30 02:12:48
【问题描述】:
所以,我有这个模板类及其专业化。
#include <iostream>
using namespace std;
template<bool> struct CompileTimeChecker{
CompileTimeChecker(...); //constructor, can accept any number of parameters;
};
//specialized template definition
template<> struct CompileTimeChecker<false> {
//default constructor, body empty
};
案例 1:
在main 函数中,我定义了一个名为ErrorA 的本地类。当我创建一个 CompileTimeChecker<false> 的临时对象并将 ErrorA 的临时对象作为初始值设定项时,编译器没有检测到任何错误。
int main()
{
class ErrorA {};
CompileTimeChecker<false>(ErrorA()); //Case 1;
CompileTimeChecker<false>(int()); //Case 2;
return 0;
}
案例 2:
接下来我用int 类型的临时对象输入它,然后编译器突然发现了这个问题(在专用模板CompileTimeChecker<false> 中没有使用args 的构造函数)
main.cpp:30:36: error: no matching function for call to ‘CompileTimeChecker::CompileTimeChecker(int)’ CompileTimeChecker<false>(int());
main.cpp:21:23: note: candidate: constexpr CompileTimeChecker::CompileTimeChecker()
template<> struct CompileTimeChecker<false> {
^~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:21:23: note: candidate expects 0 arguments, 1 provided
为什么它无法识别案例 1 中的问题?
【问题讨论】:
标签: c++11