Home c++ Type Metalling in Boost MPL

Type Metalling in Boost MPL

Author

Date

Category

I have a task:

Dana sequence of types. Check that they are all classes and streamline them so that the derivatives of the classes are before their basic. For the sequence obtained, check that all classes are derived from the latter.

Implement the solution is necessary with Boost :: MPL.
At the moment I have a swap, which changes in places 2 element in a sequence, for example, in MPL :: vector:

template & lt; TypeName SEQ, TypeName First, TypeName Second & GT;
STRUCT SWAP {
Private:
  TypeDef TypeName Begin & LT; SEQ & GT; :: Type Begin;
  TypeDef TypeName End & LT; SEQ & GT; :: Type End;
  TypeDef TypeName Clear & LT; SEQ & GT; :: Type Empty_Container;
// INSERT VALUES FROM BEGIN TO FIRST
TypeDef TypeName.
  Copy & LT;
  ITERATOR_RANGE & LT; Begin, First & GT;
  Back_inserve & LT; empty_container & gt;
  & gt; :: Type Prefix;
// INSERT SECOND VALUE
TypeDef TypeName.
  Push_Back & LT;
  Prefix, TypeName.
  Deref & LT; Second & GT; :: Type
  & gt; :: Type prefixsecond;
// INSERT VALUES FROM FIRST + 1 TO SECOND
TypeDef TypeName.
  Copy & LT;
  ITERATOR_RANGE & LT; TypeName Next & LT; FIRST & GT; :: Type, Second & GT;
  Back_inserve & LT; Prefixsecond & gt;
  & gt; :: Type prefixsecondmiddle;
// INSERT FIRST VALUE
TypeDef TypeName.
  Push_Back & LT;
  Prefixsecondmiddle, TypeName.
  Deref & LT; First & GT; :: Type
  & gt; :: Type prefixsecondmiddlefirst;
// INSERT VALUES FROM SECOND + 1 TO END
TypeDef TypeName.
  Copy & LT;
  ITERATOR_RANGE & LT; TypeName Next & LT; Second & GT; :: Type, End & GT;
  Back_inserve & LT; prefixsecondmiddlefirst & gt;
  & gt; :: type prefixsecondmiddlefirsstsuffix;
Public:
   TypeDef PrefixSecondmidDlefirstsufffix Type;
};

Here is an example of using:

TypeDef TypeName Boost :: MPL :: Next & lt; MyVector :: Begin & GT; :: Type FIRST;
TypeDef TypeName Boost :: MPL :: Next & LT; First & GT; :: Type Second;
TypeDef Swap & Lt; MyVector, First, Second & GT; :: Type Res;

and predicate that checks that the first element is the second parent:

template & lt; TypeName T1, TypeName T2 & GT;
STRUCT COMPARE_TWO {
  static const bool value = is_base_of & lt; T1, T2 & GT; :: Value;
};

Now, let’s say we have a type of type:

class base1 {};
Class Child1: Base1 {};
Class Child2: Child1 {};
Class Child3: child2 {};
TypeDef Vector & Lt; Child2, Child1, Base1, Child3 & GT; vec;

It is necessary that after applying the metafunction it turned out:
Child3, Child2, Child1, Base1

As I understand it, it is necessary to simply sort the vector by changing the corresponding types of places if the first is the ancestor of the second. Tell me how to implement this by means of Boost MPL?


Answer 1, Authority 100%

something like this:

# include & lt; boost / mpl / sort.hpp & gt;
#include & lt; boost / mpl / vector.hpp & gt;
#include & lt; Boost / MPL / Size.hpp & gt;
#Include & lt; boost / mpl / int.hpp & gt;
#include & lt; Boost / MPL / COUNT_IF.HPP & GT;
#include & lt; Boost / MPL / Equal.hpp & gt;
#Include & lt; boost / type_traits.hpp & gt;
Namespace MPL = Boost :: MPL;
Struct A {};
Struct B: A {};
STRUCT C: B {};
STRUT D: A {};
Struct E: C, D {};
// Function Sprying - Basic Class Less Natural
struct is_base_of_fn {
  TEMPLATE & LT; Class Base, Class Derived & GT; STRUCT APPLY {
    Using Type = Boost :: is_base_of & lt; base, derived & gt ;;
  };
};
STRUCT IS_CLASS_FN {
  TEMPLATE & LT; Class T & GT; STRUCT APPLY {
    Using Type = Boost :: is_class & lt; t & gt ;;
  };
};
Template & lt; Class SEQ & GT;
using is_seq_of_class = MPL :: BOOL_CLASS = MPL :: COUNT_IF & LT; SEQ, IS_Class_FN & GT; :: Type :: Value == MPL :: Size & lt; SEQ & GT; :: Value & gt ;;
Using Input_Type_set = MPL :: Vector & lt; E, B, D, C, A & GT ;;
static_assert (is_seq_of_class & lt; input_type_set & gt; :: value, "");
using outpur_type_set = TypeName MPL :: SORT & LT; INPUT_TYPE_SET, IS_BASE_OF_FN & GT; :: TYPE; 
static_assert (MPL :: Equal & Lt; Outpur_Type_set, MPL :: Vector & LT; A, B, D, C, E & GT; & GT; :: Value, "");

p.s. I recommend for reading this article (about metoprogramming, not about MPL)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions