const 成员函数指针的专门化
我正在尝试将一些实用程序代码专门用于 const 成员函数,但在使简单的测试用例正常工作时遇到问题。
为了简化工作,我正在利用 Boost.FunctionTypes 及其 components
模板 - 一个 MPL 序列,应该包含
标签 const_qualified
用于 const 成员函数。
但是使用下面的测试代码,对 const 成员函数的专门化失败了。有人知道如何让它发挥作用吗?
测试代码打印出来(使用 VC8 和 boost 1.40):
非常量
非常量
预期输出是:
非常量
常量
测试代码本身:
#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"
}
I am trying to specialize some utility code on const member functions, but have problems to get a simple test-case to work.
To simplify the work i am utilizing Boost.FunctionTypes and its components<FunctionType>
template - a MPL sequence which should contain
the tag const_qualified
for const member functions.
But using the test-code below, the specialization on const member functions fails. Does anybody know how to make it work?
The test-code prints out (using VC8 and boost 1.40):
non-const
non-const
Expected output is:
non-const
const
The test-code itself:
#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"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我仍然不清楚为什么通过
function_types::components
的方法不起作用,但我意识到有一种更简单的方法,可以使用 Boost.FunctionTypes 来专门化关于 const 成员函数:分类元函数如
is_member_function_pointer
可选地采用标签参数...While its still unclear to me why the approach via
function_types::components<>
doesn't work, i realized that there is a simpler approach with Boost.FunctionTypes to specialize on const member functions:The classification meta functions like
is_member_function_pointer<>
optionally take a tag parameter ...我没有测试过,但不
应该
I have not tested it, but shouldn't
be