Cam
Cam

Reputation: 15234

template functions in nested template classes

I'm experiencing a problem in some code I've been working on. Here is the most simplified version of it I could create:

template <class A>
class Outer {
public:
    template <class B>
    class Inner {
    public:
        template <class C>
        void foo(Outer<C>::Inner<C> innerC) { }
    };

    Inner<A> inner;
};

class X {};
class Y {};

int main() {
    Outer<X> outerX;
    Outer<Y> outerY;
    outerX.foo<Y>(outerY.inner);
}

The error is:

error: expected primary-expression before ">" token

and is triggered at compiletime at the declaration of void foo. What is incorrect about my code that is making this happen?

In words, what I am trying to do is have the nested class be able to take in a nested class with any template type - but of course the nested class's template type depends on the outer class's template type, so I use the :: syntax, but that gives an error.

I understand that what I'm trying to do here might not be a good practice, but the purpose of this question is to understand template syntax better.

Upvotes: 0

Views: 900

Answers (1)

K-ballo
K-ballo

Reputation: 81349

There is no conversion from 1 to Inner<C>. Is that an error in your reduced test case, or is it supposed to be:

template <class C>
void foo(C innerC) { }

Update: After the code was fixed, it can be seen that the problem is the lack of template before Inner<C>. Otherwise the compiler will assume that Inner is a value.

template <class C>
void foo(Outer<C>::template Inner<C> innerC) { }

Upvotes: 2

Related Questions