4

const 멤버 함수에서 일부 유틸리티 코드를 전문화하려고하지만 간단한 테스트 케이스가 작동하는 데 문제가 있습니다. const 멤버 함수에 대한 태그 const_qualifiedcontain해야 MPL 순서-
내가 Boost.FunctionTypes하고 components<FunctionType> 템플릿을 사용하고있는 작업을 단순화합니다.const 멤버 함수 포인터에 대한 전문화

하지만 아래의 테스트 코드를 사용하면 const 멤버 함수의 특수화가 실패합니다. 아무도 그것을 작동하게하는 방법을 알고 있습니까?

테스트 코드 프린트 아웃 (VC8를 사용하여 1.40 부스트)

비 CONST
비 CONST

예상 출력된다 : 비

const
const

테스트 코드 자체 :

#include <iostream> 
#include <boost/function.hpp> 
#include <boost/bind.hpp> 
#include <boost/function_types/function_type.hpp> 
#include <boost/mpl/contains.hpp> 

namespace ft = boost::function_types; 
namespace mpl = boost::mpl; 

template<typename F> 
struct select 
{  
    template<bool IsConst /* =false */> 
    struct helper { 
     static void f() { std::cout << "non-const" << std::endl; } 
    }; 

    template<> 
    struct helper</* IsConst= */ true> { 
     static void f() { std::cout << "const" << std::endl; } 
    }; 

    typedef ft::components<F> components; 
    typedef typename mpl::contains<components, ft::const_qualified>::type const_qualified; 
    typedef helper<const_qualified::value> result; 
}; 

typedef boost::function<void (void)> Functor; 

template<typename MF> 
Functor f(MF f) 
{ 
    return boost::bind(&select<MF>::result::f); 
} 

class C 
{ 
public: 
    void f1() {} 
    void f2() const {} 
}; 

int main() 
{ 
    f(&C::f1)(); // prints "non-const" as expected 
    f(&C::f2)(); // prints "non-const", expected "const" 
} 
+0

것은, 내가 아직까지 테스트 솔루션을 받아 드리겠습니다 ... is_member_function_pointer<> 같은
분류 메타 기능을 선택적으로 태그 매개 변수를 사용 : const 멤버 함수에 ecialize 원래 문제. –

답변

1

을해야한다 function_types::components<>을 통해 작동하지 않는다면, 나는 더 간단한 접근법이 이라는 것을 깨달았습니다. Boost.FunctionTypes sp 내가 다른 방법을 발견하는 동안

template<typename F> 
struct select 
{  
    /* ... helper-struct as before */ 

    typedef ft::is_member_function_pointer<F, ft::const_qualified> const_qualified; 
    typedef helper<const_qualified::value> result; 
}; 
0

나는 그것을 테스트하지 않은,하지만 왜 접근하지

typedef mpl::contains<components, ft::const_qualified> const_qualified; 

내게는 아직 불분명 동안

typedef typename mpl::contains<components::type, ft::const_qualified>::type const_qualified; 
+0

이미 시도했지만 차이는 없습니다. –

+0

거기서 편집 한 것을 보았지만 구성 요소 typedef는 또한 :: type이나 내가 게시물에서했던 것과 같습니다. – leiz

+0

:: type은 meta 함수를 호출하고 결과를 얻는 것과 같습니다. 당신이 그렇게하지 않으면 메타 기능 자체를 사용합니다. – leiz

관련 문제