2017-04-17 10 views
21

Not: remove_const<deref>::type ben beklenebilir int olarak, değişken const int değilmiş gibiNeden std :: remove_const const niteleyicisini kaldırmıyor? Sadece hataları okunabilir türlerini almak için <code>std::thread</code> kullanmak

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 
} 

görünüyor.

*first bir lvalue ifadesi olduğunu
+3

Unvanınız 'remove_reference' söylüyor ama vücudunuzdaki bunu kullanmayın. –

+0

tnx, sabit, orijinal kodda her ikisini de kullanarak karıştırdım :) – NoSenseEtAl

+4

Bunun yerine [this] (http://coliru.stacked-crooked.com/a/bfccfe0a5508f107) kullanmanızı tavsiye ederim. 'std :: thread' yöntemi çünkü' const int & 'gerçek yöntemini gösterir, yönteminiz size aittir ve const int' gösterir. – nwp

cevap

30

Not, daha sonra decltype(*first) sonucu türü const int&, const int için, yani, bir referans olabilir. Referans const (int& const gibi hiçbir şey yoktur, int& const gibi bir şey olamaz), std::remove_const kullanarak aynı tür, yani const int& verecektir.

decltype specifier bakınız: bağımsız değişken ifadesinin değeri kategorisi, daha sonra decltype verimler T& lvalue ise başka tür T ekspresyonu ve

b) '

3) ise;

Birlikte std::remove_reference ile std::remove_const kullanabilirsiniz: BTW

std::remove_const<std::remove_reference<deref>::type>::type // -> int 
             ~~~~~    // -> const int & 
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  // -> const int 

: Sadece hataları okunabilir türlerini almak için std::thread kullanmak

Not:

Bu durum için doğru tipi vermediğini unutmayın.

template<typename T> 
class TD; 

ve Sen deref türünü içeren hata mesajı, örneğin alırsınız

TD<deref> td; 

olarak kullanmak: Burada Effective Modern C++ (Scott Meyers) den bunun için bir sınıf şablonu yardımcı var clang den:

prog.cc:16:11: error: implicit instantiation of undefined template 'TD<const int &>' 
TD<deref> td; 
     ^
+2

Aaave bu yüzden 'int const &' const int '' yerine 'int const 'yazmalı ve sorunun cevabı açık olmalıdır. – Mehrdad

İlgili konular