首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果函数有重载,则Boost phoenix成员函数运算符无法编译

如果函数有重载,则Boost phoenix成员函数运算符无法编译
EN

Stack Overflow用户
提问于 2019-02-28 21:08:10
回答 1查看 56关注 0票数 2

我想对有重载的类函数使用Boost phoenix成员函数运算符,比如here

以下示例失败:

代码语言:javascript
复制
#include <boost/phoenix/phoenix.hpp>
#include <boost/phoenix/operator.hpp>

using namespace std;
using namespace boost::phoenix::placeholders;

struct A
{
    int m_id = 1;
    int func() const { return 1; }
    void func(int id) { m_id = id; }
};

int main()
{
    A *a = new A;
    auto retVal = (arg1->*&A::func)()(a);

    return 0;
}

有错误:

代码语言:javascript
复制
    In function 'int main()': 17:21: error: no match for 'operator->*'
(operand types are 'const type {aka const
boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::
tag::terminal, boost::proto::argsns_::term<boost::phoenix::argument<1> >, 0l> 
>}' and '<unresolved overloaded function type>') 17:21: note: candidate is: In
 file included from /usr/include/boost/phoenix/operator/arithmetic.hpp:13:0,
 from /usr/include/boost/phoenix/operator.hpp:13, from /usr/include/boost
/phoenix/phoenix.hpp:13, from 1: /usr/include/boost/proto/operators.hpp:295:9:
 note: template<class Left, class Right> const typename 
boost::proto::detail::enable_binary<boost::proto::domainns_::deduce_domain, 
boost::proto::detail::not_a_grammar, 
boost::mpl::or_<boost::proto::is_extension<Arg>, 
boost::proto::is_extension<Right> >, boost::proto::tagns_::tag::mem_ptr, const
 Left&, const Right&>::type boost::proto::exprns_::operator->*(Left&&, 
Right&&) BOOST_PROTO_DEFINE_OPERATORS(is_extension, deduce_domain) ^ 
/usr/include/boost/proto/operators.hpp:295:9: note: template argument 
deduction/substitution failed: 17:28: note: couldn't deduce template parameter 
'Right'

但是,如果我注释掉行void func(int id) { m_id = id; },它就会像预期的那样工作。

我如何知道要使用哪种重载?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-28 21:19:54

处理(成员)函数指针到重载集总是一件痛苦的事情。您需要将地址强制转换为具有所需重载的确切签名的指针。在您的示例中,对于selection int A::func()

代码语言:javascript
复制
auto retVal = (arg1->*static_cast<int (A::*)() const>(&A::func))()(a);

或者更具可读性,但基本上是一样的:

代码语言:javascript
复制
const auto memFctPtr = static_cast<int (A::*)() const>(&A::func);
auto retVal = (arg1->*memFctPtr)()(a);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54926493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档