7

In C++ you can do this :

struct A
{
    int a, b;
    double k = 3; // OK
};

but not this

struct A
{
    int a, b;
    auto f = [this]() {return this->a + this->b; }; // Error
};

The compiler informs us of the error

non-static data member declared ‘auto’

to let us know that this would work

struct A
{
    int a, b;
    function<int(void)> f = [this]() { return this->a + this->b; }; // OK
};

I'd like to ask for some insight on this. Specifically the why (the easy answer ofourse would be that the standard explicitly does not allow it according to some verse)

To put it differently : Doesn't in class member initialization contain enough on the type of the member?

I'm aware that in class member initialization is syntactic sugar but is this a case where the compiler could harvest addtional info, but fails to do so due to design choices? (much like the lack of auto prior to C++11) or a limitation set by deeper choices (like the language being statically typed etc) ?

Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • 3
    This isn't a limitation because of static typing - your desired feature is still statically typed. I don't immediately see a reason this shouldn't/couldn't be allowed - although it probably isn't especially useful – David May 27 '14 at 16:00
  • I'm guessing there's no specific reason this isn't implemented, just that no one has stepped up with a compelling use case and proposed it. – chris May 27 '14 at 16:15
  • @chris IMHO the need to automatically deduce the type of a lambda member is a pretty good reason – Nikos Athanasiou May 27 '14 at 16:19
  • 2
    See [this ISOCPP mailing list discussion about a prototype implementation of non-static auto members](https://groups.google.com/a/isocpp.org/forum/?fromgroups=&pli=1#!searchin/std-proposals/auto$20member/std-proposals/MDyiiVR8J-s/w_hzTl056FcJ), and also the committee's discussion thereof recorded in [N3897 Auto-type members](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3897.html). – Casey May 27 '14 at 16:27
  • @Casey: That's an interesting article, I suggest you leave that comment also on the original question. – Ben Voigt May 27 '14 at 16:33
  • @Casey I was looking at the link Ben Voigt provided, thnx for the update, yours has newer material (that invalidates the answer in the other question). I could edit the question like "Is automatic member declaration going to be allowed in upcoming standards" so maybe you could elaborate on this, but I think the other question remains practically the same – Nikos Athanasiou May 27 '14 at 16:34
  • 2
    @NikosAthanasiou I doubt we'll see this feature anytime soon, there's just not much utility to be had given that the initializers cannot refer to the enclosing class really at all since they must be parsed before the class is complete. Your example `auto f = [this]() {return this->a + this->b; };` illustrates this problem: you can't complete the type of the class until you know the type of the lambda, which can't be compiled without completing the type of `this`. – Casey May 27 '14 at 16:59
  • @Casey: Similar in many respects to what I said [here](http://stackoverflow.com/a/9167827/103167). – Ben Voigt May 27 '14 at 18:45

0 Answers0