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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:24:07+00:00 2026-05-28T03:24:07+00:00

Possible Duplicate: Is there a way to prevent a class from being derived from

  • 0

Possible Duplicate:
Is there a way to prevent a class from being derived from twice using a static assert and type trait?

What I’d like to prevent is more than one of the C based template from being derived in D (i.e. there should only ever be one instance of C derived from). Was hoping for maybe a static assert in C or B that may solve this.

// My Classes
template <class T>
class A {};

class B {};

template <class T, class S>
class C : public B, public virtual A<T> {};

// Someone elses code using my classes
class D : public C<Type1, Type2>, public C<Type3, Type4>
{
};
  • 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-05-28T03:24:07+00:00Added an answer on May 28, 2026 at 3:24 am

    As it stands, it’s impossible for B or C to detect what else a more derived class inherits from, so you can’t add an assertion there. However, by adding a “curiously recursive” template parameter, you can tell C what the derived class is. Unfortunately, this does require the derived class to give the correct template argument, and there’s no way to enforce that.

    You can then determine whether the derived class inherits from B in more than one way; it is a base class, but you can’t convert a derived class pointer to B* (since that conversion is ambiguous). Note that this doesn’t necessarily indicate multiple inheritance; the test will also fail if there’s non-public inheritance.

    So the best solution I can think of is:

    #include <type_traits>
    
    template <class T> class A {};
    class B {};
    
    template <class T, class S, class D>
    class C : public B, public virtual A<T> {
    public:
        C() {
            static_assert(
                std::is_base_of<C,D>::value && std::is_convertible<D*,B*>::value, 
                "Multiple inheritance of C");
        }
    };
    
    struct Type1 {};
    struct Type2 {};
    struct Type3 {};
    struct Type4 {};
    
    class Good : public C<Type1, Type2, Good> {};
    class Evil : public C<Type1, Type2, Evil>, public C<Type3, Type4, Evil> {};
    
    int main()
    {
        Good good;
        Evil evil; // Causes assertion failure
    }
    

    I had to put the assertion in the constructor rather than the class definition, since some of the types are incomplete when the class template is instantiated. Unfortunately, this means that the error will only be reported for classes that are actually instantiated.

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

Sidebar

Related Questions

Possible Duplicate: Is there a clean way to prevent windows.h from creating a near
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: In Linux, how to prevent a background process from being stopped after
Possible Duplicate: Is there any way in C# to override a class method with
Possible Duplicate: Retrieve Windows Experience Rating Using .NET, is there a way to programmatically
Possible Duplicate: Specifying an index (non unique key) using JPA Is there a way
Possible Duplicate: Is there a way to take a screenshot using Java and save
Possible Duplicate: Is there a way to check if a var is using setInterval()
Possible Duplicate: Is there a way to the hide win32 launch console from 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.