【发布时间】:2015-12-05 15:11:14
【问题描述】:
我很惊讶下面的代码:
#include <iostream>
#include <type_traits>
using namespace std;
int main(int argc, char* argv[]) {
cout << boolalpha << is_const<const float&>::value << endl;
return 0;
}
打印false。删除引用可以正常工作:
#include <iostream>
#include <type_traits>
using namespace std;
int main(int argc, char* argv[]) {
cout << boolalpha << is_const<remove_reference<const float&>::type>::value << endl;
return 0;
}
打印出true。
两者均使用g++ -std=c++11 test.cpp 编译,使用 G++ 版本:
g++ (Ubuntu 5.3.0-1ubuntu1~14.04) 5.3.0 20151204
考虑一下,我可以理解这里有两种类型在起作用:引用类型和被引用的类型。引用的类型是const,所以第二种情况是有意义的。对于第一种情况,如果引用的类型是const,我希望它要么返回,要么总是返回true,因为引用AFAIK不能“重新分配”。
为什么它会返回false?
【问题讨论】:
标签: c++ c++11 typetraits