C++ 为什么';t std::remove_const remove const限定符?
请注意,我使用C++ 为什么';t std::remove_const remove const限定符?,c++,c++11,constants,typetraits,decltype,C++,C++11,Constants,Typetraits,Decltype,请注意,我使用std::thread只是为了获取错误中的可读类型: int main() { const int * first; using deref = decltype(*first); std::thread s = std::remove_const<deref>::type{}; // const int ??? std::thread s2 = deref{}; // const int std::thread s3 = std
std::thread
只是为了获取错误中的可读类型:
int main() {
const int * first;
using deref = decltype(*first);
std::thread s = std::remove_const<deref>::type{}; // const int ???
std::thread s2 = deref{}; // const int
std::thread s3 = std::remove_const<const int>::type{}; // int
}
intmain(){
常数int*第一;
使用deref=decltype(*第一);
std::threads=std::remove_const::type{};//const int???
std::thread s2=deref{};//const int
std::thread s3=std::remove_const::type{};//int
}
似乎
remove_const::type
是const int
,而不是我所期望的可变int
。注意*first
是一个左值表达式,那么decltype(*first)
的结果类型将是const int&
,即对const int
的引用。引用本身不是const
(它不能是const限定的,没有类似int&const
的东西),在它上面使用std::remove\u const
将产生相同的类型,即const int&
见:
T
类型的任何其他表达式,并且decltype
产生
T&
您可以将std::remove_const
与std::remove_reference
一起使用:
std::remove_const<std::remove_reference<deref>::type>::type // -> int
^^^^^ // -> const int &
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // -> const int
并将其用作
TD<deref> td;
你的标题上写着“删除引用”,但你不在身体中使用它。tnx,修复,在原始代码中使用了这两种方法,因此我将其混淆:)我建议您使用来显示类型,而不是
std::thread
方法,因为它显示的实际类型是const int&
,而您的方法取决于您,并显示const int
mutable
是一个存储类说明符,不是类型的一部分。aa这就是为什么您应该编写int-const&
而不是const-int&
。。。那么这个问题的答案是显而易见的。
TD<deref> td;
prog.cc:16:11: error: implicit instantiation of undefined template 'TD<const int &>'
TD<deref> td;
^