Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:10:12+00:00 2026-06-01T13:10:12+00:00

I have the following problem with inheritance and templates: class Base {}; class Deriv

  • 0

I have the following problem with inheritance and templates:

class Base {};
class Deriv : public Base {};

template <class T> class X{};

void f(X<Base>& inst) {}

int main()
{
  X<Base> xb;
  f(xb);
  X<Deriv> xd;
  f(xd);
  return 0;
}

The program doesn’t compile because there is not relation between X<Base> and X<Deriv>. Nevertheless I think it should be possible to do everything that can be done with X<Base> also with X<Deriv>. Is there anything that I could do other than copying the function body of f to a new function void g(X<Deriv>& inst)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:10:13+00:00Added an answer on June 1, 2026 at 1:10 pm

    Why do you think they should be related? Consider the following:

    template<typename T>
    class X;
    
    template<>
    class X<Base> {
        int x;
    };
    
    template<>
    class X<Deriv> {
        double d;
    };
    

    They’re definitely not interchangeable. So no, there is no relation between those classes and you can’t pass one to a function expecting the other. You’ll have to do something like make both types inherit from another common type that exposes the interface you need.


    Regarding your comment, you can use type traits and static_assert to do what you would do in Java:

    template<typename T>
    void f(X<T>& inst) {
        static_assert(std::is_base_of(Base, T)::value, "Template type must subclass Base");
    
        // body of function...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have following problem: class Request<T> { private T sw; public Request() { //How
I have the following problem at hand. There is a class, Foo and another
I have a problem with Visual Studio 2008 concerning virtual inheritance. Consider the following
I've got a problem with implicit casting, templates and inheritance from template classes. The
i have the following (partial) hierarchy: @MappedSuperclass public abstract class PersistentEntity { @Id @GeneratedValue(generator=system-uuid)
I have the following problem: Given a Guice type literal TypeLiteral<T> template and a
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have the following problem: I have a form in site/banen (currently local running
I have the following problem. I have three classes, A, B and C. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.