Dlaczego std::remove_const
nie konwertuje const T&
na T&
? To wprawdzie raczej wymyślony przykład pokazuje moje pytanie:std :: remove_const z odniesieniami do stałych
#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;
}
Powyższa sprawa jest banalnie łatwe do naprawienia, więc dla kontekście wyobrazić, co następuje:
#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;
}
W powyższym przykładzie, będę oczekiwać reference
do wyświetlenia, a nie constref
.
Pamiętaj, że nie ma czegoś takiego jak odwołanie do const, a jedynie odwołanie do const. – xaxxon