Search code examples
c++c++11gccclangauto

auto and copy constructor: what's wrong?


Consider the following code:

#include<queue>
#include<type_traits>

int main() {
    std::queue<int> q;
    auto p{q};
    static_assert(std::is_same<decltype(q), decltype(p)>::value, "fail");
}

It compiles fine with GCC 5.1.0 (see here) and clang 3.8.0 (see here), but it doesn't with GCC 4.9.0 (see here).
From a further analysis, it seems to be due to the fact that the type of p is deduced as std::initializer_list.
As an example, it works if one substitutes the line:

auto p{q};

With the line:

decltype(q) p{q};

I'm not sure which one is right (even though GCC 5.1.0 works according with my expectations) and that's why I've asked here.
Is it right to expect the type of p to be std::queue<int>?


Solution

  • This is a known defect in the standard that auto deduces {} as std::initializer_list. There is a proposed change to fix this defect.

    Newer gcc and clang implement the proposed resolution, whereas gcc-4.9 does not.