9

I have a problem with the following code:

template <typename U>
class lamePtr
{
public:
    typedef U* ptr;
};

template <typename U>
class smarterPointer
{
    public:
    void funFun()
    {
        typedef lamePtr<U> someType;
        someType::ptr query;
    }
};

As you see, I have a typedef inside lamePtr. Inside smarterPointer class I have a function funFun(). What am I trying to do is to make another typedef someType. Till that line, everything works fine until we get to the line with someType::ptr query.

What I want here to happen is that "query" will become lamePtr< U >::ptr (a simple value, not a typedef ;). However, I get compilation errors (with gcc 4.4.3):

temp.cpp: In member function ‘void smarterPointer&ltU>::funFun()’:
temp.cpp:15: error: expected ‘;’ before ‘query’

What am I doing wrong here?

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
Melon
  • 604
  • 1
  • 7
  • 30
  • Related, with lots of information about why the `typename` is needed: [Where and why do I have to put `template` and `typename` on dependent names?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-template-and-typename-on-dependent-names) – James McNellis Apr 02 '11 at 17:40
  • 1
    In the future, don't try to format your code with html tags. Just enter it as is, select it all, and click the `{}` button. – Benjamin Lindley Apr 02 '11 at 17:44
  • Thanks, I will keep that in mind for the future. Thanks for the reference James, I will spend some time on that – Melon Apr 02 '11 at 18:16

2 Answers2

15

someType, as lamePtr<U> is a "dependant name". It depends on what U is as to whether or not there is a member ptr and, if so, what kind of "thing" that member is.

Of course, you know that for all T, lamePtr<T>::ptr is a type, but at this stage of compilation the parser does not know that.

Use the typename keyword to hint to the parser that it's a type. The rest will be resolved later in the compilation process. Just a little C++ quirk.

template <typename U>
class lamePtr
{
public:
    typedef U* ptr;
};

template <typename U>
class smarterPointer
{
    public:
    void funFun()
    {
        typedef lamePtr<U> someType;
        typename someType::ptr query;
    }
};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
9

You need the typename keyword to signify that someType::ptr is a type.

    typename someType::ptr query;

See Officially, what is typename for? for detail.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Thanks, that worked. I will certainly have to read more about typenames, thanks for the solution and the link – Melon Apr 02 '11 at 18:15
  • @Melon : Some further reading material if you're interested: http://www.comeaucomputing.com/techtalk/templates/#typename – ildjarn Apr 02 '11 at 19:28