6

I'm writing some test cases for C++ name-demangling code, and I get a weird error when I try to compile this: (the following is pathologically bad C++ code that I would never use in practice).

template<class U, class V>
class TStruct
{
  U u;
  V v;
public:
  void setU(const U& newu) {u = newu; } 
};

template<class T, class U>
class Oog
{
    T t;
    U u;

public:
    Oog(const T& _t, const U& _u) : t(_t), u(_u) {}
    void doit(TStruct<T,U> ts1, TStruct<U,T> ts2, U u1, T t1) {}

    template<class F>
    class Huh
    {
        F f;
    public:

        template<class V>
        class Wham
        {
            V v;
        public:
            Wham(const V& _v) : v(_v) {}
            void joy(TStruct<T,V> ts1, U u, F f) {}
        };
    };
    int chakka(const Huh<T>::Wham<U>& wu, T t) {}    // error here
};

error is as follows:

 "typetest.cpp", line 165: error: nontype "Oog<T, U>::Huh<F>::Wham [with F=T]"
  is not a template

Any ideas how I can fix?

Jason S
  • 184,598
  • 164
  • 608
  • 970

3 Answers3

7

The correct line should be as,

int chakka(const typename Huh<T>::template Wham<U>& wu, T t) ...
     it's a type ^^^^^^^^         ^^^^^^^^ indicate that 'Wham' is a template

[Note: g++ is quite helpful in this case :) ]

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    thank you! I knew about `typename`, but had no idea that you had to use the `template` keyword in that way. – Jason S Sep 16 '11 at 14:07
  • @Tomalak, you give `good answer` comment and then you take back your vote/comment after someone objects it !! – iammilind Sep 16 '11 at 14:16
  • @Tomalak, [Demo](http://stackoverflow.com/questions/7359664/are-dynamic-casts-safe-to-remove-in-production-code/7359840#comment-8885562). :)) – iammilind Sep 16 '11 at 14:21
  • @iammilind: Ah. Well, first, `s/Demo/Example/` and second... sure, if someone points out a flaw in the answer that I didn't spot originally, then I'll take it back! FWIW it took a _lot_ of thought, reading and debugging and producing of _my_ final answer to come to that conclusion. [edit: wtf, all of Konstantin's input on that question has completely vanished; I have enough rep to see deleted posts, but no trace...] – Lightness Races in Orbit Sep 16 '11 at 14:22
2

You need to tell it that the Wham member of Huh will be a template:

const Huh<T>::template Wham<U> &
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
-1

This should be enough (dependent types cause troubles)

int chakka(const typename Huh<T>::Wham<U>& wu, T t) {}

Werolik
  • 923
  • 1
  • 9
  • 23