4

clang 3.0 and g++ 4.8.1 both reject the following code with the error shown in the comment:

template<typename T>
struct S
{
    void f()
    {
        this->dependent(); // no error: type of 'this' is dependent?
        this->Dependent::dependent(); // error: 'Dependent' has not been declared
    }
};

According to [basic.lookup.classref]

the class-name-or-namespace-name following the . or -> operator is looked up both in the context of the entire postfix-expression and in the scope of the class of the object expression.

And [temp.dep.expr]

this is type-dependent if the class type of the enclosing member function is dependent.

If the class-or-namespace-name Dependent is looked up 'in the scope of the class of the object expression* this, and the class of the object expression is dependent, should this lookup not be deferred until the template is instantiated? Does the standard specify the correct behaviour?

EDIT: clang 3.0 accepts the following code, but g++4.8 gives the same error as above

template<typename T>
struct S
{
    T m;
    void f()
    {
        m.dependent();
        m.Dependent::dependent();
    }
};
willj
  • 2,991
  • 12
  • 24
  • I don't see how `this->Dependent::dependent()` could be a dependent expression. How does it depend on the template parameter? – mfontanini Jul 07 '13 at 21:35
  • [temp.dep.expr] "`this` is type-dependent if the class type of the enclosing member function is dependent". – willj Jul 07 '13 at 21:45
  • The second code make sens as it could depend on the template but not the first. For me, both the first and the second code compile fine on clang3, g++4.8.1 and msvc_110 unless of course I try to instantiate the first code – a.lasram Jul 07 '13 at 21:54
  • clang 3.0 or 3.2? clang 3.0 definitely rejects the first code fragment. – willj Jul 08 '13 at 22:21
  • See http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords/17579889#17579889 – Johannes Schaub - litb Jul 11 '13 at 08:00

1 Answers1

2

In your first code, both lines are "illformed, no diagnostic required" because "this" refers to the current instantiation, but no member was found and the class template has no dependent base classes.

It is neither a member of the current instantiation, nor of an unknown specialization. See 14.6.2.1p6

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Which version of the standard are you quoting? I guess this language was added since C++03. – willj Jul 10 '13 at 10:02
  • @willj even in C++03 it was "illformed, NDR" however there was no specific rule for it, just the general "if no valid specialization can be generated, the template is ill-formed; NDR". Now we have specific rules. – Johannes Schaub - litb Jul 10 '13 at 11:57
  • So a class counts as a 'compound type' composed of its 'sequence of members and base class objects', in which case `this` is dependent if its type has a dependent base-class? – willj Jul 10 '13 at 13:15
  • @willj "this" is always dependent in a template. But that does not mean that `this->foo` necessarily is dependent. – Johannes Schaub - litb Jul 10 '13 at 13:25