小编daf*_*ito的帖子

带有const引用的std :: remove_const

为什么不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)

c++ templates std c++11

17
推荐指数
1
解决办法
7092
查看次数

标签 统计

c++ ×1

c++11 ×1

std ×1

templates ×1