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.
}
This codes does not compile with gcc 4.8.0.
I don't even guess the type of decltype(x3)
. What is it? And why the behavior is different?
#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");
}
This will compile. x3
is deduced to be a std::initializer_list<int>
due to:
Let
T
be the type that has been determined for a variable identifierd
. ObtainP
from T [...] if the initializer is a braced-init-list (8.5.4), withstd::initializer_list<U>
.
So x3
is actually an std::initializer_list<int>
, one way for you to have figured this out would be as follows:
std::cout << typeid(x3).name() << std::endl ;
for me I had the following output:
St16initializer_listIiE
Putting this through c++filt
:
c++filt -t St16initializer_listIiE
Gives me:
std::initializer_list<int>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With