tags:

views:

81

answers:

2

Hi, I tried this :

#ifndef __TEST__
#define __TEST__

namespace std
{
    template<typename T>
    class list;
}

template<typename T>
void Pop(std::list<T> * l)
{
    while(!l->empty())
        l->pop();
}

#endif

and used that function in my main. I get errors. Of course, I know that there are more template params for std::list (allocator I think). But, that is beside the point. Do I have to know the full template declaration of a template class to be able to forward declare it?

EDIT: I wasn't using a pointer before - it was a reference. I'll try it out with the pointer.

+1  A: 

Forward declaration should have complete template arguments list specified.

Grozz
+3  A: 

The problem is not that you can't forward-declare a template class. Yes, you do need to know all of the template parameters (and their defaults) to be able to forward-declare it correctly, but to make even such a forward declaration in namespace std is explicitly prohibited by the standard: the only thing you're allowed to put in std is a template specialisation, commonly std::less on a user-defined type. Someone else can cite the relevant text if necessary.

Just #include <list> and don't worry about it.

Oh, incidentally, any name containing double-underscores is reserved for use by the implementation, so you should use something like TEST_H instead of __TEST__. It's not going to generate a warning or an error, but if your program has a clash with an implementation-defined identifier, then it's not guaranteed to compile or run correctly: it's ill-formed. Also prohibited are names beginning with an underscore followed by a capital letter, among others. In general, don't start things with underscores unless you know what magic you're dealing with.

Jon Purdy
Thanks for the tip!!!
nakiya
Why is it prohibited to forward declare things in `namespace std` btw?
nakiya
Check out this answer (http://stackoverflow.com/questions/307343/forward-declare-an-stl-container/307408#307408) and the linked newsgroup discussion.
Jon Purdy
Jon / Nakiya, why not use `#pragma once` rather than the #ifdef's. It's supported by most compilers these days.
Mark Ingram
@Mark: Because it's `#pragma`, that's why. Though it is an option.
Jon Purdy
What's the issue with `#pragma`?
Mark Ingram
There are scrillions of duplicates of that question. Just search: http://stackoverflow.com/search?q=pragma+once
Jon Purdy