I think this is a g ++ error with member initialization by default. I am not sure about this and therefore have the following supporting evidence:
template<class T=void> struct c { std::function<int(int)> f; c() : f([](int i){return i+i;}) { } }; int main() {}
If this works, then what you do should work too. And this happens even if you build c .
Personally, I believe that default member initialization should be used sparingly and with care. I think it is very easy to create a lot of confusion with it, because most people expect all initialization to be done in the constructor, and element initializers are not necessarily next to any constructor. In this way, they can leave someone scratching their heads, wondering how a member gets special meaning.
I see cases, especially with simple, mostly data classes, for which it will work very well. But basically, I think that if you have a constructor body of any type, you probably shouldn't use the default member initialization.
source share