Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

You can use decltype on any valid C++ expression, therefore your 3 statements are correct (it has nothing to do with static or non static modifiers).

Section 7.1.6.2 of the standard says

The type denoted by decltype(e) is defined as follows:

  • if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e . If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
  • otherwise, if e is an xvalue, decltype(e) is T&& , where T is the type of e ;
  • otherwise, if e is an lvalue, decltype(e) is T& , where T is the type of e ;
  • otherwise, decltype(e) is the type of e.

For standard references, see stroustrup FAQ here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top