Const 회원 기능 포인터에 대한 전문화
-
19-09-2019 - |
문제
Const 회원 기능에 대한 일부 유틸리티 코드를 전문으로하려고하지만 간단한 테스트 케이스를 작동시키는 데 문제가 있습니다.
내가 활용하는 작업을 단순화하기 위해 boost.functionTypes 그리고 그것 components<FunctionType>
템플릿 - a MPL 시퀀스 해야 할 일 contain
태그 const_qualified
const 멤버 함수의 경우.
그러나 아래 테스트 코드를 사용하면 Const 회원 기능에 대한 전문화가 실패합니다. 아무도 작동하는 방법을 아는 사람이 있습니까?
테스트 코드는 인쇄합니다 (VC8 및 부스트 1.40 사용) :
비정규
비정규
예상 출력은 다음과 같습니다.
비정규
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"
}
해결책
왜 접근이 왜 function_types::components<>
작동하지 않습니다. boost.functionTypes const 멤버 기능을 전문화하려면 :
분류 메타 기능은 다음과 같습니다 is_member_function_pointer<>
선택적으로 a 꼬리표 매개 변수 ...
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;
};
다른 팁
나는 그것을 테스트하지 않았지만해서는 안됩니다
typedef mpl::contains<components, ft::const_qualified> const_qualified;
~이다
typedef typename mpl::contains<components::type, ft::const_qualified>::type const_qualified;
제휴하지 않습니다 StackOverflow