2014-12-21 2 views
-1

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의 두 번째 인수에 태그를 바인딩하려고 시도했다. 불운!

+0

여기서 'funct_type'은 어떻게 사용됩니까? – 0x499602D2

+0

내가 알고 싶은 thats. 어떻게 사용합니까? : -/ – Pogo

+0

일반 기능으로 어떻게했는지 보여줄 수 있습니까? 또한 왜 오버로딩을 피하기를 원합니까? 이것은 태그 파견을위한 완벽한 기회입니다. – 0x499602D2

답변

3

문제의 복잡성을 이해할 수 없으므로 요점을 놓쳤거나 사례를 지나치게 복잡하게 만들었습니다 ... 왜 다른 펑터를 만들고 조건부와 함께 사용할 것인지 선택하십시오.

struct functor1 { … }; 
struct functor2 { … }; 

using functor = std::conditional<condition, functor1, functor2>::type; 

std::for_each(v.begin(), v.end(), functor()); 
+0

아마도 함수 객체를 가진 함수 디스패치 메소드를 복잡하게 만들지 않아야합니다. 이것은 간단한 아이디어 였고이 방향으로 생각할 수 없었습니다. 지금은 저에게 유용 할 것입니다. 고마워요. – Pogo