mardi 26 mai 2015

SFINAE enable/disable function and template alias

I was wondering how to achieve what I am describing below.
Consider a base CRTP class with a function that needs to be enabled and another one disabled.
This is controlled via a traits class dependent upon the definition of a type or not.
I preferably want to not have to define anything in my traits class but if it helps defining it as void I am OK with that. Further information in comments as it is much easier to show there what I am trying to achieve.

template<class T>
struct Model_Traits;

// Base CTRP class
template<typename Model>
class BaseModel
{
  inline const Model& impl() const { return static_cast<Model const&>(*this); }

  // not sure how to enable this template or not based on my traits class
  // which is necessary as some wont have it defined
  using foo_t = typename Model_Traits<Model>::foo_t;

  // this one should be enabled when foo_t is not defined
  void optimize(const foo1& f1, const foo2& f2, foo3* f3)
  {
    impl().optimize(f1,f2,f3);
  }

  // this one should only be enabled if foo_t is defined
  // not sure if this is correct
  template<typename T = foo_t,
           typename = std::enable_if<foo_t>::type>
  void optimize(const foo1& f1, const foo2& f2, foo3* f3, foo_t* f4)
  {
    impl().optimize(f1,f2,f3, f4);
  }
}

// First class defining the foo_t
template<MyModel>
struct Model_Traits
{
   using foo_t = myFoo;
}

class MyModel : public BaseModel<MyModel>
{
   void optimize(const foo1& f1, const foo2& f2, foo3* f3, foo_t* f4);
}

// Second class not defining foo_t
template<MyModel2>
struct Model_Traits
{

}

class MyModel2 : public BaseModel<MyModel2>
{
   void optimize(const foo1& f1, const foo2& f2, foo3* f3);
}

I should of course say that this is simplified and my code does not look like that but its pretty close if you take all the other stuff out.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire