相关疑难解决方法(0)

boost :: bind与受保护的成员和上下文

在下面的代码中,有两个std::for_each使用boost:bind表达式的"等效"调用.指示的行编译,指示的失败行失败.我能在标准中找到的最佳解释相当于"因为我们这么说".我正在寻找"为什么标准表明这种行为".我的假设在下面.

我的问题很简单:为什么指示的行编译和等效的后续行无法编译(我不想因为"标准这样说",我已经知道 - 我不会接受任何给出这个的答案解释;我想解释标准为何如此说明.

注意:虽然我使用boost,但boost与此问题无关,并且使用g ++ 4.1.*和VC7.1重现了各种格式的错误.

#include <boost/bind.hpp>
#include <iostream>
#include <map>
#include <algorithm>

class Base
{
protected:
        void foo(int i)
        { std::cout << "Base: " << i << std::endl; }
};

struct Derived : public Base
{
        Derived()
        {
                data[0] = 5;
                data[1] = 6;
                data[2] = 7;
        }

        void test()
        {
                // Compiles
                std::for_each(data.begin(), data.end(),
                        boost::bind(&Derived::foo, this,
                                boost::bind(&std::map<int, int>::value_type::second, _1)));

                // Fails to compile - why?
                std::for_each(data.begin(), data.end(),
                        boost::bind(&Base::foo, this,
                                boost::bind(&std::map<int, …
Run Code Online (Sandbox Code Playgroud)

c++

6
推荐指数
1
解决办法
1802
查看次数

标签 统计

c++ ×1