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
    类型的任何其他表达式,并且
  • b) 如果表达式的值类别为左值,则
    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;
              ^