vendredi 29 mai 2015

Using Type Traits from Base Class

I am trying to understand the concept of type traits.

Say i have some templatized Class Hierachy like this and a client function:

template<typename T>
class Base
{
public:
//...
    virtual bool inline isSymmetric() const = 0;
};


template<typename T>
class ChildrenOperation:public Base<T>
{
public:
//...
    virtual bool inline isSymmetric() const override
    {
        return true;
    }
};

void clientFunction(const Base& operation)
{
  //do symmetry independent stuff...
  if(operation.isSymmetric())
  { //use operation the one way 
  } else { //use operation the other way
  }
}

Obviously, clientFunction is polymorphic and different children can have different implementations of isSymmetric. However, since isSymmetric seems to be constant and really more of a type information, i've read about type traits and i was wondering whether it is possible to rewrite the client function to not depend on isSymmetric on runtime, but rather compile time.

I've tried adding a trait like this. But i am not sure how to specialize it and use it in a polymorphic context.

template <typename T>
struct is_symmetric {
  static const bool value = false;
};

Aucun commentaire:

Enregistrer un commentaire