为什么add_const<T>中的T若为引用则add_const<T>::type仍为T?

为什么add_const对引用无效,比如我认为,add_const<int &>::type应该是const int &才对,
而实际上,add_const<int &>::type仍旧是int &

阅读 3.5k
2 个回答

引用只有底层const,没有顶层const。
假如add_const<int &>对int &产生改变,则其type会是int &const,显然是不对的

template <typename T, 
    bool = is_reference<T>::value ||
    is_function<T>::value ||
    is_const<T>::value
>
struct add_const;

然后写了true和false的特化

要加reference有一个add_lvalue_referenceadd_rvalue_reference

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题