-1

How to determine if a class has no class deriving from it? Any type-traits methods help?

I want to write out some data. The data is created via a pointer like

unique_ptr<A> a(new A);

If class A has no sub-class, I just write out the data content. If A has subclasses, I will write out its concrete type first, and then write the data content. When reading the data, after reading the concrete type, I know using what concrete type to create the pointer and then read the data content. Thus, I think I have to determine if A has subclass or not?

user1899020
  • 13,167
  • 21
  • 79
  • 154

2 Answers2

1

Thats not possible, because a class doesn't know what (If exists) class derives from it.

What you could check is if a class derives from another:

struct foo {};

struct bar : foo {};

static_assert( std::is_base_of<foo,bar>::value );
Manu343726
  • 13,969
  • 4
  • 40
  • 75
1

In C++11, you can declare a class final. This makes sure that no class can derive from it. See http://en.cppreference.com/w/cpp/language/final.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62