Jump to content
  • Advertisement
Sign in to follow this  
KennithTheMenith

Template member and inheritance

This topic is 4971 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
Advertisement
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
Try to change the line to “this->template foo<int>();” and see if it works.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!