【发布时间】:2017-07-08 17:18:24
【问题描述】:
这是我的程序,它尝试使用 std::conditional 根据整数模板参数的值设置成员变量的类型。
#include <stdio.h>
#include <type_traits>
using namespace std;
class cool{
public:
cool(){ printf("Cool!\n"); }
};
class notcool{
public:
notcool(){ printf("Not cool!\n"); }
};
template<int N>
class myclass{
public:
typedef std::conditional<N==5,cool,notcool> thetype;
thetype theinst;
};
int main(){
printf("Testing\n");
myclass<5> myc5;
myclass<6> myc6;
printf("Done testing\n");
return 0;
}
我希望我的程序给出以下输出:
测试
酷!
不酷!
完成测试
相反,输出是
测试
完成测试
我的编译器是 GCC 4.9,我编译这个程序的方式是使用命令 g++ test -std=c++11 -o test
谁能告诉我为什么程序没有给出我期望的输出?
【问题讨论】:
-
这些是构造函数,应该在实例化对象 myclass 时自动调用。
标签: c++ c++11 templates typetraits