mercredi 1 mars 2017

Declaring variable with name `this` inside lambda inside parentheses leads to different results on 3 different compilers

In C++ it's possible to declare variable inside parentheses like int (x) = 0;. But it seems that if you use this instead of variable name, then constructor is used instead: A (this); calls A::A(). So the first question is why it's different for this, is it because variables can't be named this? And to complicate matters a bit lets put this inside a lambda -

struct B;
struct A
{
  A (B *) {}
};

struct B 
{
  B ()
  {
    [this] { A (this); } ();
  }
};

Now gcc calls A::A(), msvc prints error about missing default constructor and clang prints expected expression (http://ift.tt/2lc7LXg). It's even funnier in msvc - it really creates variable with name this that you can use, so it's definitely a bug (http://ift.tt/2lWmfHP). Which compiler is right and what are the reasons for such behavior?

Aucun commentaire:

Enregistrer un commentaire