c++typedefforward-declaration

"using typedef-name ... as class" on a forward declaration


I'm doing some policy-based designs here and I have the need to typedef lots of template types to shorten the names.
Now the problem comes that when I need to use a pointer to one of those types I try to just forward-declare it but the compiler complains with a test.cpp:8: error: using typedef-name ‘Test1’ after ‘class’
It's nothing to do with sizes as I don't need the obj at all, its just a pointer in a ".h" file where I don't want to bring the whole template in.
This is g++:

//Works
class Test{};
class Test;

//Doesn't work
class Test{};
typedef Test Test1;
class Test1;

Any hint?


Solution

  • That's correct. A typedef-name cannot be used in such a forward declaration (this is technically called an elaborated type specifier, and if such a specifier resolves to a typedef-name, the program is ill-formed).

    I don't understand why you actually need the forward declaration in the first place. Because if you already have the typedef in place, why not just use that typedef? It's just aswell denoting that class.

    Edit: From your comment, it seems you need a designated forward declaration header, much in the same vain of <iosfwd>. So, if you template is called Test_Template_Name_Long, this a header can look like

    TestFwd.h

    template<typename A, typename B> 
    class Test_Template_Name_Long;
    
    typedef Test_Template_Name_Long<int, bool> Test1;
    

    Then you can just use that header, instead of doing class Test1, which the compiler has no clue about what it is (and will think it is a new class, independent of any template and typedef).