为什么不std::remove_const转换const T&成T&?这个公认的相当人为的例子证明了我的问题:
#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;
}
Run Code Online (Sandbox Code Playgroud)
上述情况很容易解决,因此对于上下文,请想象以下内容:
#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* …Run Code Online (Sandbox Code Playgroud)