C ++ template function taking template class as parameter

I am struggling with the following code. Basically, I have a Foo class and a nested Bar class, and now I want to pass a pointer to the Bar class of the object of the function, but it does not compile. Can anyone help me with this? Thanks.

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; } 
+7
source share
2 answers

You must have the following signature

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

However, this is not the only problem.

 func(&foo.bar_); 

should also be

 func<int>(&foo.bar_); 

This is because you are calling the func boilerplate function, but its type cannot be inferred. Without its type, it will throw an error, for example

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

This is a dependent name , you must say:

 template <typename T> void func(typename Foo<T>::Bar* bar) // Tell the compiler explicitly it a type 
+3
source

All Articles