Class constructor placement

Why is this code incorrect?

class Method { public: Method(decltype(info2) info1); virtual ~Method(){} protected: QSharedPointer<info> info2; }; 

But this code is correct:

 class Method { public: virtual ~Method(){} protected: QSharedPointer<info> info2; public: Method(decltype(info2) info1); }; 

why is the place of class constructor important? I thought the place of the constructor of the definition class is not important.

+5
source share
2 answers

I believe this part of the standard is relevant [basic.scope.class] /1.1 :

The potential scope of the name declared in the class consists not only of the declarative region following the names of the points of declaration, but also of all function bodies, default arguments, exception specification s, as well as parentheses or equal initializers of non-static data in this class ( including such things in nested classes).

Note that it only mentions the default arguments. Thus, this works as decltype is referenced in the default argument:

 Method(QSharedPointer<int> info1 = decltype(info2)()) 

And this also works, since inside the body:

 Method(<...>) { decltype(info2) info3; } 

However, your example does not work, because such a placement of the decltype object does not apply to the paragraph I have given, so the name info2 is considered out of scope.

+1
source

Location QSharedPointer info2;

important. 'info2' must be defined before using it in decltype ( http://en.cppreference.com/w/cpp/language/decltype ).

Further will not work:

 void f() { d(); } void d() { } 
0
source

Source: https://habr.com/ru/post/1264828/


All Articles