SFINAE를 사용하여 개체를 기능화하려고했습니다. 예를 들면 :다른 operator() 메서드를 사용하여 함수 개체에 SFINAE를 사용하는 방법
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);
do stuff with vertex
(기본적으로 operator()
)는 펑터 태그에 따라 다른 내가 태그를 기반으로이 세 가지 펑 또는 std::conditional, or std::enable_if or any nested condition checker.
내가 할 수 있었다 같은 다른 방법을 과부하 할 내 표준 기능에 대해 동일한 것을 달성하십시오. 그러나 나는 펑터를 위해 그것을하는 방법을 모르겠습니다. 문제는 펑터는 operator() 구현에서만 다르며 operator() 메소드를 과부하하고 싶지 않다는 것입니다. 기능
태그 파견 :
struct func1_tag{ };
struct func2_tag{ };
void do_this(int x)
{
do_this_actually(int x, dispatch());
}
do_this_actually(int x, func1_tag) { //do stuff }
do_this_actually(int x, func2_tag) { // ... }
typedef std::conditional<true, func1_tag, func2_tag>::type dispatch;
이 기능을위한 것입니다. 하지만 어떻게 함수 객체에 사용하는지 모르겠습니다. 나는 functor의 두 번째 인수에 태그를 바인딩하려고 시도했다. 불운!
여기서 'funct_type'은 어떻게 사용됩니까? – 0x499602D2
내가 알고 싶은 thats. 어떻게 사용합니까? : -/ – Pogo
일반 기능으로 어떻게했는지 보여줄 수 있습니까? 또한 왜 오버로딩을 피하기를 원합니까? 이것은 태그 파견을위한 완벽한 기회입니다. – 0x499602D2