为什么std::remove_const
不能将const T&
转换为T&
?这个不可否认的虚构的例子演示了我的问题:std :: remove_const与常量引用
#include <type_traits>
int main()
{
int a = 42;
std::remove_const<const int&>::type b(a);
// This assertion fails
static_assert(
!std::is_same<decltype(b), const int&>::value,
"Why did remove_const not remove const?"
);
return 0;
}
上述案例是十分容易解决,所以对于背景下,想象一下以下内容:
#include <iostream>
template <typename T>
struct Selector
{
constexpr static const char* value = "default";
};
template <typename T>
struct Selector<T&>
{
constexpr static const char* value = "reference";
};
template <typename T>
struct Selector<const T&>
{
constexpr static const char* value = "constref";
};
int main()
{
std::cout
<< Selector<typename std::remove_const<const int&>::type>::value
<< std::endl;
return 0;
}
在上面的例子中,我期望reference
被显示,而不是constref
。
请记住,没有这样的东西作为const引用,只有const的引用。 – xaxxon 2016-07-14 09:19:17