dimanche 29 mars 2015

Unexpected behaviour with overload resolution when using std::initializer_list with a boolean overloaded function

I am trying to use an initialization list with different overloaded function as shown in the example code below. It seems that the boolean overload and the array overload has an exclusive existentialism with the mapped overload.



#include <string>
struct Spam
{
Spam();
Spam(bool flag); //(1)
Spam(const std::initializer_list<std::pair<const std::string, Spam > > & il); //(2)
Spam(const std::initializer_list<Spam > & il); //(3)
};
int main()
{
Spam({ { "1", Spam() }, { "2", Spam() } });
}


That means in the above code both Spam(bool flag) and Spam(const std::initializer_list<Spam > & il) cannot co-exist when using the mapped version of initialization list Spam(const std::initializer_list<std::pair<const std::string, Spam > > & il). Compiler complains



error C2440: '<function-style-cast>' : cannot convert from 'initializer-list' to 'Spam'


This behaviour was initially observed with VC12 and later was replicated with G++ [See Demo] and Clang [See Demo] The behaviour is unexpected. What is the reason for this strange behaviour and how it can be resolved?


Aucun commentaire:

Enregistrer un commentaire