【发布时间】:2014-03-16 10:26:07
【问题描述】:
int main(int argc, char* argv[]) {
const int i = 10;
using Type = typename std::conditional<false, int, int&>::type;
const Type r = i; // It seems this 'const' does not have any effect.
std::cout << r << std::endl;
}
上面的代码在gcc4.8.1 -std=c++11上无法编译,错误信息如下:“invalid initialization of type of type {aka int&}' from expression of 'const int' . 但是,如果我像这样更改代码,它将起作用:
int main(int argc, char* argv[]) {
const int i = 10;
using Type = typename std::conditional<false, const int, const int&>::type;
Type r = i;
std::cout << r << std::endl;
}
有没有人可以告诉我原因?
【问题讨论】:
标签: c++ c++11 std typetraits