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 8504619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:05:10+00:00 2026-06-11T02:05:10+00:00

Suppose I have class D which inherits from class B . and I have

  • 0

Suppose I have class D which inherits from class B. and I have a template class template <class T> C. And then I have a function signature void foo(C<B> c);. In order to make that work with an instance of C templated with a derived class from B do I need to provide a conversion constructor? Something like template <class DERIVED> C(const C<DERIVED>& c) {}? Will this be enough for anything else that happens in C since any D is a B?

  • 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-11T02:05:12+00:00Added an answer on June 11, 2026 at 2:05 am

    C<D> and C<B> will not be related types at all, so yes, you’d have to provide a conversion that allows a C<B> to be constructed from a C<D>.

    This probably will not make the types perfectly compatible. For example a function void foo(C<B> &c) will not be able to use that conversion.

    Whether these unrelated types can be made compatible enough for your uses depends on how you define the types and what you need from them in terms of compatibility.


    struct B {};
    struct D : B {};
    
    template<typename T>
    struct C {};
    
    int main() {
        C<B> c = C<D>();
    }
    

    test1.cpp:9:10: error: no viable conversion from 'C<struct D>' to 'C<struct B>'
        C<B> c = C<D>();
             ^   ~~~~~~
    

    Consider this example for why there should not be any relationship between different specializations of a template:

    struct B {};
    struct D : B {};
    
    template<typename T> struct C;
    
    template<> struct C<B> { int i; }; // different specializations
    template<> struct C<D> { double a, b, c; }; // can have different definitions
    

    Converting between types related by inheritance works because a derived type always has a sub-object of the base type. Even so, types that use inheritance must be carefully designed in order to work well.


    User defined conversions operate via non-explicit constructors and type operators:

    struct T {};
    
    struct S {
        S() {}
        S(T const &) {}
        operator T () { return T(); }
    };
    
    void foo(T t) {}
    void bar(S s) {}
    
    int main() {
        foo(S()); // uses S::operator T ()
        bar(T()); // uses S::S(T const &)
    }
    

    You can read more in the standard 12.3.2 Conversion functions [class.conv.fct] and about how they are used in overload resolution 13.3 Overload resolution [over.match].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have abstract class A and abstract class B which inherits from A,
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
Suppose I have an autolocker class which looks something like this: template <T> class
Lets Suppose that I have made a custom authentication module that inherits IHttpModule class.
Suppose that I have class Foo and class FooFrobber { private Foo _foo; FooFrobber(Foo
Suppose I have some class which has a property actor_ of type Actor .
Suppose I have a class A which depends on 3 other classes X, Y
Suppose I have a file X.h which defines a class X, whose methods are

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.