According to the C++ Primer, C++ arrow operator yields an lvalue. Additionally decltype
of an expression which yields an lvalue will result in a reference type. So why the following decltype does not result in a reference type.
struct MyStruct {
string name
};
MyStruct s;
s.name = "aname";
MyStruct* p = &s;
decltype (p -> name) str = s.name; //type of str will be string and not &string although p -> name yields an lvalue
From cppreference
If the argument is an unparenthesized id-expression or an unparenthesized class member access, then decltype yields the type of the entity named by this expression. If there is no such entity, or if the argument names a set of overloaded functions, the program is ill-formed.
This is the case in your example, so it will return the underlying type of the member, which is a std::string
.
If you want, you can add parenthesis so that decltype
results in a reference:
//'str' is a std::string&
decltype((p->name)) str = s.name;
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