views:

55

answers:

2

Consider the following program:

template <typename T>
struct t
{
    struct base { void f1(); };
    struct derived : base
    {
        void f2()
        {
            f1();
        }
    };
};

In derived::f2, unqualified lookup is used to find f1. Will base be searched? Will base::f1 be found? Is base a dependent type?

Please substantiate your answers with quotes from the standard.

+7  A: 

Yes, base is dependent so f1 is not found. In C++03 this was clearified by the addition 14.6.1/2d (which did not exist in C++98) and in C++0x this is directly stated by 14.6.2.1/6 (n3126).

Dependency of it is important, because the following is possible

// for T = int, f1 does not exist.
template<> struct t<int>::base { };
Johannes Schaub - litb
Ah, that's the stone I missed, I had forgotten about the possibility to only specialize a sub-class of a template type.
Matthieu M.
Thanks, 14.6.1/2d is exactly what I was looking for!
avakar
@litb: it seems to me that there is something about the look-up of functions that only those declared before the point of use (in the template) shall be considered. I suppose this doesn't apply to member functions, somehow, because it's never been a prerequisite for members to be declared before their point of use ?
Matthieu M.
@Matthieu I don't understand your question.
Johannes Schaub - litb
@litb: `void f(int); template <typename T> g() { f(4.0); } void f(double);`, `g` should invoke `f(int)` here (if I recall correctly) whereas in the case exposed by avakar the base could be specialized after the definition of `f2`... isn't there something like this on look-up to prevent further includes to somehow "corrupt" a method definition.
Matthieu M.
@Matthieu the base class could be specialized after the definition of `f2` but not after instantiation of `A<X>::derived`. When `derived` is instantiated, then in turn the correct specialization of `base` is selected and (possibly) instantiated. A rule says that after `base` was instantiated, no explicit specialization that would have been selected as the specialization cannot be provided anymore. So after you say for example `sizeof (t<Foo>::derived)`, `t<Foo>::base` cannot be explicitly specialized anymore.
Johannes Schaub - litb
A: 

This doesn't fit into a comment box...

gcc rejects the code:

there are no arguments to 'f1' that depend on a template parameter, so a declaration of 'f1' must be available.

However introducing this-> before f1 makes it dependent and then the compilation works.

As far as my experience has shown, the same behavior apply to:

  • types (you have to typedef them again)
  • methods (you need to use using base::f1 or this->f1)

And I have seen this behavior in:

  • gcc 3.4
  • VC++ 9
  • VC++ 10

And now we can wait for standardistas to get exact quotes from the standard.

Matthieu M.
Thanks for answering! Actually, introducing `this->` before `f1` makes the search use a member name lookup instead of an unqualified lookup, so the name is found. I'm asking, because I'm trying to compile our codebase with clang and it failed on something which can be reduced to the sample in the question (whereas MSVC and apparently gcc-4.3 accepted it).
avakar
MSVC does not correctly implement template support (it doesn't implement the two-phases look-up mandated by the standard). I shall test with gcc-4.3 --> it's accepted (gcc-4.3.2) I wonder if it's been solved since.
Matthieu M.
@avakar: I am excited to see that many people are interested in Clang. I am myself and I really think it could become a great compiler with time (and effort).
Matthieu M.