samedi 20 décembre 2014

how to I use SFINAE for function objects with different operator() methods

I wanted to use SFINAE to function objects. For example:



struct functor_1_tag{ };
struct functor_2_tag{ };
struct functor_3_tag{ };

template<typename Graph_t>
struct functor {
functor() { };
functor(Graph_t& G) : m_G(G) { }

template<typename vertex_t>
void operator(vertex_t vertex) {
/// do stuff with vertex
}
private:
Graph_t& m_G;
};

// Similary another 2 functors that have operator() doing different tasks.

//Usecase:
// just for 2 functors here
typedef std::conditional <true, functor_1_tag, functor_2_tag>::type funct_type;

/// functor --> shud be selected based upon conditional above.??
auto func = std::bind(functor<graph>, G, std::placeholders::_1);

// use in algorithm for vertices
std::for_each(vertexset.begin(), vertexset.end(), func );


The do stuff with vertex ( essentially the operator()) is different based upon the functor tags and I want to overload these three functors based upon tags or any other methods such as std::conditional, or std::enable_if or any nested condition checker.


I was able to achieve the same thing for my standard functions. but I am not sure how to do it for functors. The thing is that functors differ only in their operator() implementation and I dont want to overload the operator() methods.


Aucun commentaire:

Enregistrer un commentaire