In 5.1.1/3 of C++ standard [expr.prim.general]
Unlike the object expression in other contexts, *this is not required to be of complete type for purposes of class member access outside the member function body. Only class members declared prior to the declaration are visible.
And then this example:
struct A {
char g();
template<class T> auto f(T t) -> decltype(t + g())
{ return t + g(); }
};
template auto A::f(int t) -> decltype(t + g());
Can you explain the quote and the example? What exactly is being demonstrated here?
Whenever the definition of a class member appears outside of the class declaration, the member name must be qualified by the class name using the :: (scope resolution) operator. The following example defines a member function outside of its class declaration.
If a member function's definition is outside the class declaration, it is treated as an inline function only if it is explicitly declared as inline . In addition, the function name in the definition must be qualified with its class name using the scope-resolution operator ( :: ).
Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.
The definition of member functions can be inside or outside the definition of class. If the member function is defined inside the class definition it can be defined directly, but if its defined outside the class, then we have to use the scope resolution :: operator along with class name alng with function name.
It means that you can access members via this
, either explicitly or implicitly, outside function bodies within a class definition. At that point, the type is incomplete, and usually you can't access members of incomplete types.
But you can only do that within restricted parts of a member function declaration; the previous sentence says about this
:
It shall not appear before the optional cv-qualifier-seq
meaning that you can't use it in a parameter or leading return type specification. As far as I can see, the only place you can use it, outside a function body, is in a trailing return type.
You might need to do this when using decltype
in a trailing return type, to get the type of an expression involving a non-static member. The example demonstrates this by implicitly using this
to access g()
in the trailing return type. It would have been more clear what was being demonstrated if it were written as decltype(t + this->g())
.
What exactly is this an example of? Which statement is being demonstrated here?
The statement being demonstrated is that:
Unlike the object expression in other contexts, *this is not required to be of complete type for purposes of class member access (5.2.5) outside the member function body.
Outside the body of the member function there is an invocation to g()
, which means this->g()
. There, the type of *this
(i.e. A
) is not complete.
Per paragraph 9.2/2 of the C++11 Standard:
A class is considered a completely-defined object type (3.9) (or complete type) at the closing
}
of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, and brace-or-equal-initializers for non-static data members (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With