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

浏览:30日期:2023-05-24

问题描述

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

问题解答

回答1:

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

回答2:

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

然后写了true和false的特化

要加reference有一个add_lvalue_reference和add_rvalue_reference

相关文章: