Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program compiling despite using nonexistent members

template <typename T>
void list<T>::copyAll(const list &l)
{
    if(l.isEmpty()) //if the list-to-copy is empty, we're done
    {
        first = last = NULL;
    }
    else
    {
        node *toFollow = l->yhjfrtydfg;
        node *whatever = l.asfqwejfq3fqh23f8hq23r1h23017823r087q1hef;

        while(toFollow != NULL)
        {
            T *newO = new T(*(toFollow->o));
            T here = *newO;
            insertBack(&here);
            toFollow = toFollow->next;
        }
    }
}

This program compiles (with the rest of the program), even though the two lines node *toFollow = l->yhjfrtydfg; and node *whatever = l.asfqwejfq3fqh23f8hq23r1h23017823r087q1hef; are clearly crazy input. It's strange because any other error is caught. Any help?

like image 263
Bob John Avatar asked Jul 24 '26 00:07

Bob John


1 Answers

"Dependent names" (names used in templates whose meaning depends on the template parameters) are only resolved when the template is instantiated, not when it's defined. If you don't instantiate the template, then it can contain whatever crazy names you like, as long as it's syntactically correct.

Your program would fail to compile if it were instantiated:

list<int> l;
l.copyAll(l); // ERROR
like image 178
Mike Seymour Avatar answered Jul 26 '26 14:07

Mike Seymour