对 const 的引用等同于 std::is_const 是什么?
What's the equivalent of std::is_const for references to const?
考虑代码:
int const x = 50;
int const& y = x;
cout << std::is_const<decltype(x)>::value << endl; // 1
cout << std::is_const<decltype(y)>::value << endl; // 0
这是有道理的,因为 y
不是 const
引用,它是对 const
的引用。
是否存在 foo
使得 std::foo<decltype(y)>::value
为 1?如果没有,定义我自己的会是什么样子?
#include <string>
#include <iostream>
#include <type_traits>
using namespace std;
int main()
{
int const x = 50;
int const& y = x;
cout << std::is_const<std::remove_reference<decltype(x)>::type>::value << endl; // 1
cout << std::is_const<std::remove_reference<decltype(y)>::type>::value << endl; // 1
return 0;
}
考虑代码:
int const x = 50;
int const& y = x;
cout << std::is_const<decltype(x)>::value << endl; // 1
cout << std::is_const<decltype(y)>::value << endl; // 0
这是有道理的,因为 y
不是 const
引用,它是对 const
的引用。
是否存在 foo
使得 std::foo<decltype(y)>::value
为 1?如果没有,定义我自己的会是什么样子?
#include <string>
#include <iostream>
#include <type_traits>
using namespace std;
int main()
{
int const x = 50;
int const& y = x;
cout << std::is_const<std::remove_reference<decltype(x)>::type>::value << endl; // 1
cout << std::is_const<std::remove_reference<decltype(y)>::type>::value << endl; // 1
return 0;
}