c++ - std::make_pair 错误与 boost::bind 在 VS2010

标签 c++ visual-c++ boost visual-c++-2010 boost-bind

我想知道是否有人可以帮助我解决这个问题。我一直在读到在 VS2010 中使用 std::make_pair 存在一些问题,因为它重载了,我发现了一些可行的解决方法,但是,我只是找不到一种方法让它在这里工作我。

这是部分代码,您可以看一下:

namespace tree {
    #define container std::vector
    typedef container<IConnType const*> node_data;

///tree node's brief
    struct tree_node
    {
        STD_STRING  name;
        node_data   types;
    };

    struct branch;
    typedef container<branch>           sub_tree;

///branch's brief
    struct branch
    {
        tree_node   node;
        sub_tree    tree;
    };
}



template<typename T>
///address of's brief
struct address_of
{
    T* operator()(T& x) const
    {
        return &x;
    }

    T const* operator()(T const& x) const
    {
        return &x;
    }
};



typedef std::pair<tree::branch*,HTREEITEM> step_info;
std::vector<step_info> steps;

/// after we fill steps ///

HTREEITEM new_item = m_conntree.InsertItem(&tvi); // m_conntree is a CTreeCtrl; tvi is a TVINSERTSTRUCT


std::transform(step.first->tree.begin()
    , step.first->tree.end()
    , std::back_inserter(steps)
    , boost::bind(&std::make_pair<tree::branch*,HTREEITEM>
        , boost::bind<tree::branch*>(address_of<tree::branch>()
            , _1
        )
    , new_item
    )
);

问题就出在这里(其余代码仅供引用):

std::transform(step.first->tree.begin()
    , step.first->tree.end()
    , std::back_inserter(steps)
    , boost::bind(&std::make_pair<tree::branch*,HTREEITEM>
        , boost::bind<tree::branch*>(address_of<tree::branch>()
            , _1
        )
    , new_item
    )
);

我试着做了一个转换(正如我在其他线程中读到的那样)但是它没有用......这是我尝试过的:

typedef std::pair<tree::branch*,HTREEITEM> (*MakePairType)(tree::branch*,HTREEITEM);

std::transform(step.first->tree.begin()
    , step.first->tree.end()
    , std::back_inserter(steps)
    , boost::bind((MakePairType)&std::make_pair<tree::branch*,HTREEITEM>
        , boost::bind<tree::branch*>(address_of<tree::branch>()
            , _1
        )
    , new_item
    )
);

我希望任何人都可以帮我解决这个问题......我已经被困了很长时间试图编译这个项目......

顺便说一下,它在 boost::bind 中给我带来了很多错误(超过一百个)...并且去掉了 boost::bind,它让我不知道哪个 std 重载的错误: :make_pair 使用,

此致,并提前致谢!

最佳答案

Dave S 是对的:lambda 或仿函数在这里会好得多。您在使用 make_pair 时遇到的问题可能是由于 a breaking change in C++11 . make_pair 现在有 T&&U&& 类型的参数,以实现 pair 类型的完美转发。

你像这样使用make_pair:

std::make_pair<tree::branch*,HTREEITEM>

因为您已经明确命名了模板类型参数,所以参数类型选择为 tree::branch*&&HTREEITEM&&。此函数不能接受左值参数。

简而言之:不要尝试使用make_pair 或其他带有显式模板参数列表的完美转发函数;它们并非设计为以这种方式使用。

关于c++ - std::make_pair 错误与 boost::bind 在 VS2010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10710062/

相关文章:

c++ - 这个 pData[1+2*i]<<8|pData[2+2*i] C++ 语法是什么意思?

c - 从目标文件中提取单个过程?

c++ - 处理类和类的模板函数*

c++ - boost::ref 没有发生匹配调用错误,但 std::ref 没有

Android NDK - 如何类型转换对象类

c++ - 如何使用 C++ 制作双重排序整数数组?

c++ - 寻找高分辨率计时器

c++ - 尝试读取 4 字节无符号整数的二进制文件并转换为伏特

c++ - boost::adaptors::strided 不能在boost::adaptors::transformed 之后使用?

c++ - Boost Enum 256 元素限制