C++ template function taking template class as parameter

c++parameterstemplates

I'm struggling with the following code. Basically, I have a class Foo and nested class Bar, and now I want to pass a pointer of class Bar object to a function, but it doesn't compile. Could anyone help me with this? Thank you.

template <typename T>
struct Foo
{
    struct Bar
    {
        T data_;
    };
    Bar bar_;
};

template <typename T>
void func(Foo<T>::Bar* bar) // Why is this line wrong???
{
}

int main()
{
    Foo<int> foo;
    foo.bar_.data_ = 17;
    func(&foo.bar_);
    return 0;
}

Best Solution

You need to have the following signature

template <typename T>
void func(typename Foo<T>::Bar* bar) // Why is this line wrong???

However, that is not the only problem

func(&foo.bar_);

also needs to be

func<int>(&foo.bar_);

This is because you are calling the templated function "func" but its type can not be deduced. Without its type, it will give an error such as

no matching function for call to 'func(Foo<int>::Bar*)'