Search code examples
c++c++11decltype

Is the expression 'decltype(MyTag::non_static_m.test + 1)' syntactically valid?


The question is the commented lines' in the code below:

struct MemberType
{
    int test;
};

struct MyTag
{
    MemberType non_static_m;// Note that it's NOT defined with 'static'.
};

int main(void)
{
    typedef decltype(MyTag::non_static_m) TestType_Good;// Well-formed in C++11.
    typedef decltype(MyTag::non_static_m.test) TestType_1;// Is it right or wrong?
    typedef decltype(MyTag::non_static_m.test + 1) TestType_2;// Is it right or wrong?

    return 0;
}

Note that the member in 'MyTag' is a non-static field. I would be grateful if relevant clauses in the ISO standard documents were listed.


Solution

  • This is valid. In unevaluated operands (decltype, sizeof among others) you can name nonstatic datamembers without an object expression, within arbitrary subexpressions. Note that that this does not apply to nonstatic member functions, but only to data members.