mercredi 5 août 2015

Class templated member function and return type inference

I wanted to use the C++11 auto little specifier to deduce my return type , but the example in which i am trying to use it is a little different than its typical use.

I have a base class , and subclasses that derive certain member functions.

enum class state {composite1, composite2, composite3};
class material
{
public:

}
class sub_material1 : material
{
public:
 template<typename T>
 T* transform(state to_state)
 {
 T* material;
   switch(to_state){
      case(state::composite1) :
         //do transform computations
         // set material to return
         material = some_transformed_material;
         break;
      case(state::composite2):
         // do transform computation
         // set material to return
         material = some_transformed_material;
         break;
.
.
.
.
}
return material;
}
}

so if i had something like

sub_material1 mat1;
sub_material2 mat2;

mat2 = mat1.transform(state::composite2);

how can i use auto and decltype in this case given the fact that i have condition to test that can be very lengthy....decltype seems an overkill ?

How can the type be inferred ?

Thank you

Aucun commentaire:

Enregistrer un commentaire