dimanche 19 juillet 2020

Dependent name and scope

Does anyone know what is going on with the compiler when certain dependent functions are in scope after vs before use? I'm using a close copy of Stroustrup's example in C++ 4th Ed Page 747. In the DEP_NAME example g and Q can be declared after the template function f and are in scope, however in the non DEP_NAME example the opposite is true.

Appreciate your guidance!

#define DEP_NAME
#ifdef DEP_NAME
template<typename T> T f(T a)
{
    return g(a); // OK: a is a dependent name and therefore so is g
}
// can be declared after f
class Q { };
Q g(Q e)
{
    return e;
}
#else
// must be declared before f
class Q { };
Q g(Q e)
{
    return e;
}
template<typename T> T f(T a)
{
    return g(Q{});
}
#endif

int main(int argc, char *argv[])
{
    Q z = f(Q{});
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire