Question

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

Was it helpful?

Solution

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";
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top