首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >困惑于提升精神/凤凰/ C++11互动

困惑于提升精神/凤凰/ C++11互动
EN

Stack Overflow用户
提问于 2013-03-10 19:01:50
回答 1查看 1.1K关注 0票数 2

我已尽我所能减少这个问题。

如果我取消注释void initialize(),那么这段代码将编译。如果我忽略了它,那么它就不会构建。

解决这个问题的唯一方法是用C++03模式构建boost::shared_ptr而不是std::shared_ptr

我尝试使用on上的stock编译器(使用libc++)和CentOS 6.4 x64上的以下编译器:

代码语言:javascript
复制
/opt/llvm/3.2/bin/clang++ -gcc-toolchain /opt/gcc/4.7.2 -std=gnu++11 foo.cc -I/opt/boost/1.53.0/include -DBOOST_SPIRIT_USE_PHOENIX_V3 -DBOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT -DBOOST_SPIRIT_DEBUG
/opt/llvm/3.1/bin/clang++ -gcc-toolchain /opt/gcc/4.7.2 -std=gnu++11 foo.cc -I/opt/boost/1.53.0/include -DBOOST_SPIRIT_USE_PHOENIX_V3 -DBOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT -DBOOST_SPIRIT_DEBUG
/opt/llvm/3.0/bin/clang++ -gcc-toolchain /opt/gcc/4.7.2 -std=gnu++11 foo.cc -I/opt/boost/1.53.0/include -DBOOST_SPIRIT_USE_PHOENIX_V3 -DBOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT -DBOOST_SPIRIT_DEBUG
/opt/gcc/4.7.2/bin/g++ -std=gnu++11 foo.cc -I/opt/boost/1.53.0/include -DBOOST_SPIRIT_USE_PHOENIX_V3 -DBOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT -DBOOST_SPIRIT_DEBUG
/opt/gcc/4.7.1/bin/g++ -std=gnu++11 foo.cc -I/opt/boost/1.53.0/include -DBOOST_SPIRIT_USE_PHOENIX_V3 -DBOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT -DBOOST_SPIRIT_DEBUG

与往常一样,spirit的编译器输出非常冗长,因此我将其作为一个要点包括在内:

守则如下..。

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

class Object {
    public:
        void initialize(std::vector<int>) {
        }

        //void initialize() {
        //}
};

int main() {
    boost::spirit::qi::rule<std::string::iterator, int()> integer;
    boost::spirit::qi::rule<std::string::iterator, std::shared_ptr<Object>()> object;

    using boost::phoenix::bind;
    using boost::spirit::_val;
    using boost::spirit::_1;

    object  
        = (*integer) [bind(&Object::initialize, *_val, _1)];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-10 19:08:17

代码语言:javascript
复制
#define BOOST_SPIRIT_USE_PHOENIX_V3

帮我修好。并将*val更改为val,因为菲尼克斯将知道如何将成员函数绑定到它。

正如@llonesmiz所暗示的那样,更新了,这实际上与ADL有关。虽然这种关系非常微妙。

  • 在此过程中,std::vector<>在成员函数指针类型中的存在使得ADL搜索std名称空间并找到std::bind,而不是phoenix::bind
  • 不知何故,当您传递val而不是*val时,编译器会选择菲尼克斯bind作为更好的匹配。
  • 您可以看到,当您有一个成员函数,例如,接受一个int (而不是来自std命名空间的类型)时,问题就消失了,并且总是选择菲尼克斯绑定。

您可以通过检查这个最小的测试程序的输出来查看上面的观察,该输出转储各种绑定表达式的类型(并通过c++filt运行它们)。

  • http://liveworkspace.org/code/idDtv$0上看GCC的现场直播
  • 更新 Clang似乎只喜欢使用boost::shared_ptr而不是:http://liveworkspace.org/code/idDtv$3。我只能假设这是由于libc++的差异(?)
代码语言:javascript
复制
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/phoenix.hpp>
#include <memory>

class Object {
    public:
        void initialize(std::vector<int>) {
        }
};

int main() {
    boost::spirit::qi::rule<std::string::iterator, int()> integer;
    boost::spirit::qi::rule<std::string::iterator, std::shared_ptr<Object>()> object;

    using boost::phoenix::bind;
    using boost::spirit::_val;
    using boost::spirit::_1;

    object  
        = (*integer) [bind(&Object::initialize, _val, _1)];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15326235

复制
相关文章

相似问题

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