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
.但为什么?
的const
上值参数时不影响任何形状或形式的签名.从非模板声明中删除引用时也是如此.该const
只影响在函数定义中使用的参数.如果添加引用或指向类型的指针,则更改并const
影响函数的类型.
在你的嵌套类型T
是int&
到该const
应用.然而,T&
和T& const
也是相同的类型.我猜你的困惑源于你const
在左边的不明智的位置:它更const
适用于顶级实体.