为什么const在这个模板结构中丢失了?

iva*_*ult 9 c++ templates language-lawyer

众所周知,以下函数指针具有不同的类型:

void foo_int_ref(int&);
void foo_const_int_ref(const int&);

static_assert(
    !std::is_same<
        decltype(foo_int_ref),
        decltype(foo_const_int_ref)
    >::value,
    "Types should be different");
Run Code Online (Sandbox Code Playgroud)

让我们考虑一下这种概括:

template<typename T>
void foo(T t) {}

template<typename T>
struct ref_vs_const_ref {
    typedef decltype(foo<T>) foo_T;
    typedef decltype(foo<const T>) foo_const_T;
};

using int_ref_vs_const_ref = ref_vs_const_ref<int&>;

static_assert(
    !std::is_same<
        typename int_ref_vs_const_ref::foo_T,
        typename int_ref_vs_const_ref::foo_const_T
    >::value,
    "Types should be different"); // -- it fails
Run Code Online (Sandbox Code Playgroud)

最后一个断言失败了.由于某种原因,const失去了foo_const_T.但为什么?

Die*_*ühl 5

const上值参数时影响任何形状或形式的签名.从非模板声明中删除引用时也是如此.该const只影响在函数定义中使用的参数.如果添加引用或指向类型的指针,则更改并const影响函数的类型.

在你的嵌套类型Tint&到该const应用.然而,T&T& const也是相同的类型.我猜你的困惑源于你const在左边的不明智的位置:它更const适用于顶级实体.

  • @ivaigult:当`T`是`int&`时,`const T`做*不*转换为`const int&`而是转换为`int&const`. (5认同)
  • @ivaigult - `const T`没有转换为`const int&`,就像它不会转换为`const int*`for`T = int*`. (3认同)