Sign in to follow this  

Template member and inheritance

This topic is 4778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am having a problem inheriting from a template class with a template member function, minimal example below:
#include <iostream>

template<typename T>
struct A
{
  template<typename U>
  void foo() const
  {
    std::cout << typeid(U).name() << '\n'; // just do something
  }
  // rest of class ...
};

template<typename T>
struct B : public A<T>
{
  void bar() const
  {
    foo<int>(); // line X 
  }
};

int main()
{
  B<float> b;
  b.foo<int>();
  b.bar();  
}

If I comment out line X this compiles and runs as expected. If not then I get a parse error (using gcc 3.3) Is it possible to call an inherited template member function from within another member function? Any explanation or references would also be appreciated - I have not been having much joy trying to look this one up - am I just missing something really obvious? Thanks K

Share this post


Link to post
Share on other sites
I guess it has to do with your compiler, I tried out your code on Borland C++ Builder 6.0 and it compiles and run perfectly.

As a comment, I had to
 #include <typeinfo> 
in order to use
typeid
is this your case too?

Share this post


Link to post
Share on other sites

This topic is 4778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this