const 成员函数指针的专门化

发布于 2024-08-15 05:27:56 字数 2208 浏览 10 评论 0原文

我正在尝试将一些实用程序代码专门用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-08-22 05:27:56

虽然我仍然不清楚为什么通过 function_types::components 的方法不起作用,但我意识到有一种更简单的方法,可以使用 Boost.FunctionTypes 来专门化关于 const 成员函数:
分类元函数如 is_member_function_pointer 可选地采用标签参数...

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;
};

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 ...

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;
};
葬シ愛 2024-08-22 05:27:56

我没有测试过,但不

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

应该

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

I have not tested it, but shouldn't

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

be

typedef typename mpl::contains<components::type, ft::const_qualified>::type const_qualified;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文