2013-05-22 11 views
11
int main(){ 
    int x{}; 
    auto x2 = x; 
    auto x3{x}; 

    static_assert(is_same<int, decltype(x)>::value, "decltype(x) is the same as int"); 
    static_assert(is_same<int, decltype(x2)>::value, "decltype(x2) is the same as int"); 
    static_assert(is_same<int, decltype(x3)>::value, "decltype(x3) is the same as int"); // Error here. 
} 

Kod ten nie jest kompilowany z gcc 4.8.0. Nawet nie odgaduję typu decltype(x3). Co to jest? I dlaczego zachowanie jest inne?Dlaczego auto jest wyprowadzane inaczej?

+0

deklaracji .... –

+0

@GrijeshChauhan można być bardziej konkretnego? – Sungmin

+5

'auto x {y}' jest 'std :: initializer_list'. – Xeo

Odpowiedz

12
#include <initializer_list> 
#include <type_traits> 

using namespace std; 

int main(){ 
    int x{}; 
    auto x2 = x; 
    auto x3{x}; 

    static_assert(is_same<int, decltype(x)>::value, "decltype(x) is the same as int"); 
    static_assert(is_same<int, decltype(x2)>::value, "decltype(x2) is the same as int"); 
    static_assert(is_same<std::initializer_list<int>, decltype(x3)>::value, "decltype(x3) is the same as int"); 
} 

To skompiluje. wyprowadza się std::initializer_list<int> powodu:

Let T być typem, który został określony przez zmienną identyfikatora d. Uzyskaj P od T [...], jeśli inicjalizator jest listą ze wzmocnionymi początkami (8.5.4), z std::initializer_list<U>.

+0

Dzięki. Mam to :) – Sungmin

6

Więc jest rzeczywiście std::initializer_list<int> jeden sposób, aby mieć zdobione ten byłby następujący:

std::cout << typeid(x3).name() << std::endl ; 

dla mnie miałem następujący wynik:

St16initializer_listIiE 

Putting to przez c++filt:

c++filt -t St16initializer_listIiE 

daje mi: błędy

std::initializer_list<int> 
+0

Dziękuję, mam to :) – Sungmin

Powiązane problemy