jeudi 5 mars 2015

Initialization and lambda-type argument

I have such a utility class:



struct Atreturn
{
std::function<void()> funcdestr;
Atreturn( std::function<void()> fd ): funcdestr(fd) {}
~Atreturn() { funcdestr(); }
};


Note no explicit attribute at constructor.


Possible use should be:




  1. Direct-initializing constructor call:



    Atreturn hook ( [something]() { DestroySomething(something); } );



  2. Copy-initializing constructor call:



    Atreturn hook = [something]() { DestroySomething(something); };



  3. Direct-list-initializing constructor call:



    Atreturn hook { [something]() { DestroySomething(something); }};



Now the question: to my best knowledge, the method #1 and #2 should be allowed as they are theoretically the same thing, provided that there's no explicit at the constructor, while #3 should not be allowed because this syntax prevents conversions (at least it's so for int if you tried int{2.1}).


However, gcc 4.9 allows method #1 and #3, but not #2 (and says conversion from '...::<lambda()>' to non-scalar 'Atreturn' type requested). This sounds crazy because it normally happens only if you have explicit constructor. Can anybody explain, why?


Aucun commentaire:

Enregistrer un commentaire