std :: remove_const mit const Referenzen
Warum tutstd::remove_const
nicht konvertierenconst T&
zuT&
? Dieses zugegebenermaßen eher konstruierte Beispiel zeigt meine Frage:
#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;
}
Der obige Fall ist trivial einfach zu beheben. Stellen Sie sich für den Kontext Folgendes vor:
#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;
}
Im obigen Beispiel würde ich erwartenreference
gezeigt werden, anstattconstref
.