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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:52:19+00:00 2026-05-31T18:52:19+00:00

I would like to ask you about some advices about this code. It works,

  • 0

I would like to ask you about some advices about this code. It works, but I think it can be written in a more elegant way. It’s a C++11 piece of code, so keep it in mind when you compile it 😉 !

#include <iostream>
#include <type_traits>
#include <typeinfo>

using namespace std;

class A {};
class B: public A {};

class C {};
class D: public C {};

class E: public A, public C {};

template<class T, typename = void>
class Example;

template<class T>
class Example<T, typename enable_if<is_base_of<A, T>::value and not is_base_of<C, T>::value>::type>
{
  public:
    string a() const
    {
      return string(typeid(T).name()) + " have A as base class";
    }
};

template<class T>
class Example<T, typename enable_if<not is_base_of<A, T>::value and is_base_of<C, T>::value>::type>
{
  public:
    string b() const
    {
      return string(typeid(T).name()) + " have C as base class";
    }
};

template<class T>
class Example<T, typename enable_if<is_base_of<A, T>::value and is_base_of<C, T>::value>::type> :
    public Example<A>,
    public Example<C>
{
};

int
main()
{
  Example<B> example1;
  Example<D> example2;
  Example<E> example3;

  cout << example1.a() << endl;
  //cout << example1.b() << endl;   It must raise a compile error
  //cout << example2.a() << endl;   It must raise a compile error
  cout << example2.b() << endl;
  cout << example3.a() << endl;
  cout << example3.b() << endl;
}

As you can see I’m trying to write a class template that can handle classes derived from A and C. The problem is when A and C are either inherited as for class E. In fact we could also have something like this…

template<class T>
class Example<T, typename enable_if<is_base_of<A, T>::value> { /* ... */ };

template<class T>
class Example<T, typename enable_if<is_base_of<C, T>::value> { /* ... */ };

…but it will fail when a class (like E) inherits either A and C.

Any ideas for a better code?
Thanks

  • 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-31T18:52:21+00:00Added an answer on May 31, 2026 at 6:52 pm

    The simpler way is to use a static_assert.

    template <typename T>
    class Example
    {
    public:
        std::string a() const
        {
            static_assert(std::is_base_of<A, T>::value, "T must derive from A to use a()");
            return std::string(typeid(T).name()) + " have A as base class";
        }
    
        std::string b() const
        {
            static_assert(std::is_base_of<C, T>::value, "T must derive from C to use b()");
            return std::string(typeid(T).name()) + " have C as base class";
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask about some advices, how to use Symfony2 SonataAdminBundle. I
There are some discussions about the same question but I would like to ask
i would like to ask about some Notepad++ feature that i couldn't find in
I would like to ask to you to share some ideas about how do
I would like to ask a theoritical question about how some web sites work.
I would like to ask about the available (free or not) Static and Dynamic
I would like to ask for some simple examples showing the uses of <div>
I would like to ask for a reccomended solution for this: We have a
I would like to ask how licensing works on multiple instances? Suppose that I
I would like to ask is there any way to achieve this functionality: I

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.