I have the following code:
#include <initializer_list>
#include <utility>
enum class Classification
{
Unspecified,
Primary,
Secondary
};
class ClassificationMap
{
public:
ClassificationMap(std::initializer_list<std::pair<const Classification, int>> content = {});
};
void foo(ClassificationMap) {}
int main()
{
foo({{Classification::Unspecified, 42}});
}
Visual Studio 2013 & 2017 and Clang 3.4.1 (and above) both compile the code just fine. From my POV, it should be fine as well. However, GCC 5.1 refuses to compile it, with the following error:
<source>: In function 'int main()':
<source>:22:44: error: could not convert '{{Unspecified, 42}}' from '<brace-enclosed initializer list>' to 'ClassificationMap'
foo({{Classification::Unspecified, 42}});
(I am passing the correct standard flag (-std=c++11
) to both GCC and Clang).
Is there a problem in my code, or is this actually a GCC bug?
Supplemental info: in my real code, the initialiser list is used to initialise an unordered map member of the ClassificationMap
class (that's why its type is what it is). I need the code to work in VS2013 & GCC 5.1
{}
around them makes it work. Very strange indeed... – Palgrave= {}
inClassificationMap(std::initializer_list<std::pair<const Classification, int>> content = {})
gets it to compile in g++ – Retouchfoo(std::initializer_list<std::pair<const Classification, int>>{{Classification::Unspecified, 42}});
It seems that gcc fais to correctly deduce the type ofstd::initializer_list
. Clang handles it just fine. – Pleiad-std=c++11
? – Pfaff-std=c++11
is used. – Pegboardstd::pair
(there's no such contructor), which is what theClassificationMap
constructor takes. The extra braces "solve" the problem because that direct-list-initializes the whole thing. – Yanezfoo({{make_pair(Classification::Unspecified, 42)}});
– Bobstay