[Development] How to make a fancy QMap c++17 initializer_list

Marc Mutz marc.mutz at kdab.com
Wed Jul 26 00:03:17 CEST 2017


On 2017-07-25 10:10, Grégoire Barbier wrote:
> Le 24/07/2017 à 21:44, Henry Skoglund a écrit :
>> On 2017-07-24 21:32, Grégoire Barbier wrote:
>>> Le 24/07/2017 à 18:45, Henry Skoglund a écrit :
>>>>      QMap m1 = {std::pair{2,"score"}, {4,"seven"}, {1,"Four"}, 
>>>> {5,"years"}, {3,"and"}, {6,"ago"}};
>>> 
>>> Or even this:
>>> 
>>> QMap<int, const char*> m1 { {2, "score"}, {4, "seven"}, };
>>> 
>> 
>> Yes that works nicely too but it's not bleeding edge/c++17 :-)
>> 
>> This post was just to show that in c++17 it's possible to declare a 
>> QMap without any trailing <> i.e. letting the compiler figure out the 
>> types (less visual clutter) More here: 
>> http://en.cppreference.com/w/cpp/language/class_template_argument_deduction
>> 
>> Rgrds Henry
> 
> Sorry I missed your point, by far.
> 
> Anyway it's not easy: I played a little bit on godbolt.org and locally
> on my computer and it seems that gcc 7.1 only deduces templates
> argument with a depth of 1 (never with templates of templates), ICC 17
> and MSVC 15.2 being worst on this point (not deducing at all, if I did
> not miss something).
> 
> Even when addind deduction guides like I tried here:
> https://godbolt.org/g/pC8SaV
> 
> So, I'll wait and keep my explicit template arguments if needed a few
> more years, and keep being thankful to all pioneers like you who use
> it now and will make compilers better... :-)

A nested brace initializer does not have a type, if the types used in 
the inner braces are distinct, so

    std::map m = {{k1, v1}, {k2, v2}};

will not work, even if the compiler implements the DR that you find 
referenced from the cppreference link Henry posted. AFAIU, this would 
need more language support than what we find in C++17. Ville can 
probably tell if there are proposals on the table to enable this.

Thanks,
Marc




More information about the Development mailing list