Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help with type traits

Suppose we have the following template class

template<typename T> class Wrap { /* ... */ };

We can not change Wrap. It is important.

Let there are classes derived from Wrap<T>. For example,

class NewInt  : public Wrap<int>     { /* ... */ };
class MyClass : public Wrap<myclass> { /* ... */ };
class Foo     : public Wrap<Bar>     { /* ... */ };

We can not change these classes too. All classes above is 3rd party. They are not mine.

I need the following compile time type_traits:

template<class T>
struct is_derived_from_Wrap { 
     static const bool value = /* */;
};

What do I need?

assert(is_derived_from_Wrap<Int>::value == true);  // Indeed I need static assert
assert(is_derived_from_Wrap<MyClass>::value == true);
assert(is_derived_from_Wrap<char>::value == false);
struct X {};
assert(is_derived_from_Wrap<X>::value == false);
like image 546
Alexey Malistov Avatar asked Jan 14 '10 07:01

Alexey Malistov


People also ask

What is a type trait?

A type modifier or transformation trait is a template that takes one or more template arguments and has one member, type , which is a synonym for the modified type.

What is a trait class C++?

Traits are used throughout the C++ library. A trait is a class or class template that characterizes a type, possibly a template parameter. At first glance, it seems that traits obscure information, hiding types and other declarations in a morass of templates.

What is type support?

There are basically three types of support and these are roller, pinned, and fixed support. There is a fourth support also called as simple support, it is generally not used in structures. Every support has its own field of application.


1 Answers

You can do this using SFINAE but its kind of magical if you dont know whats going on...

template<typename T> class Wrap { };

struct myclass {};
struct X {};

class Int     : public Wrap<int>     { /* ... */ };
class MyClass : public Wrap<myclass> { /* ... */ };

template< typename X >
struct is_derived_from_Wrap
{
  struct true_type { char _[1]; };
  struct false_type { char _[2]; };

  template< typename U >
    static true_type test_sfinae( Wrap<U> * w);
  static false_type test_sfinae( ... );

  enum { value = sizeof( test_sfinae( (X*)(0) ) )==sizeof(true_type) };
};


#include <iostream>
#define test(X,Y) std::cout<<( #X " == " #Y )<<"  : "<<( (X)?"true":"false") <<std::endl;

int main()
{
  test(is_derived_from_Wrap <Int>::value, true);
  test(is_derived_from_Wrap <MyClass>::value, true);
  test(is_derived_from_Wrap <char>::value, false);
  test(is_derived_from_Wrap <X>::value, false);
}

This gives the expected output

is_derived_from_Wrap <Int>::value == true  : true
is_derived_from_Wrap <MyClass>::value == true  : true
is_derived_from_Wrap <char>::value == false  : false
is_derived_from_Wrap <X>::value == false  : false

There are a couple of gotchas with my code. It will also return true if the type is a Wrap.

assert(  is_derived_from_Wrap< Wrap<char> >::value == 1 );

This can probably be fixed using a bit more SFINAE magic if needed.

It will return false if the derivation is not a public derivation (i.e is private or protected )

struct Evil : private Wrap<T> { };
assert( is_derived_from_Wrap<Evil>::value == 0 );

I suspect this can't be fixed. (But I may be wrong ). But I suspect public inheritance is enough.

like image 190
Michael Anderson Avatar answered Sep 20 '22 16:09

Michael Anderson