简体   繁体   English

VS2010中带有boost :: bind的std :: make_pair错误

[英]std::make_pair error with boost::bind in VS2010

I was wondering if someone could help me with this issue. 我想知道是否有人可以帮助我解决这个问题。 I have been reading that the are some problems with the use of the std::make_pair in VS2010 because it is overloaded, and I have found some workarounds that would work, however, I just can not find a way to make it work here for me. 我一直在阅读,因为在VS2010中使用了std :: make_pair会导致一些问题,因为它已超载,并且我发现了一些可行的解决方法,但是,我只是找不到一种方法来使其在这里工作我。

Here is a part of the code so you can take a look: 这是代码的一部分,因此您可以看一下:

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

The problem is here (the rest of the code is just to give an idea): 问题出在这里(其余代码仅是一个想法):

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

I tried to do a cast (as I had read in other thread) but it did not work... this is what I had tried: 我尝试进行强制转换(就像我在其他线程中阅读的那样),但是它没有用……这是我尝试过的:

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

I hope anyone can help me with this one... I have been stuck for a long time trying to compile this project... 我希望任何人都可以帮助我解决这个问题。我在尝试编译这个项目时已经坚持了很长时间……

By the way, it throws me a lot of error in the boost::bind (more than a hundred)... and taking out the boost::bind, it gives me errors about not knowing which overload of std::make_pair to use, 顺便说一句,它在boost :: bind中引发了很多错误(一百多个)...并取出boost :: bind,这给了我关于不知道std :: make_pair哪个重载的错误。采用,

Regards, and thanks in advance! 问候,并预先感谢!

Dave S is right: a lambda or a functor would be far better here. Dave S是对的:λ或函子在这里要好得多。 The problem you are running into with make_pair is likely due to a breaking change in C++11 . make_pair遇到的问题可能是由于C ++ 11中的重大更改所致。 make_pair now has parameters of type T&& and U&& , to enable perfect forwarding of the pair type. make_pair现在具有T&&U&&类型的参数,以实现对类型的完美转发。

You're using make_pair like so: 您正在像这样使用make_pair

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

Because you've explicitly named the template type parameters, the parameter types are selected as tree::branch*&& and HTREEITEM&& . 因为您已经明确命名了模板类型参数,所以将参数类型选择为tree::branch*&&HTREEITEM&& This function cannot accept lvalue arguments. 该函数不能接受左值参数。

In short: do not try using make_pair or other perfect-forwarding functions with explicit template argument lists; 简而言之:不要尝试使用带有显式模板参数列表的make_pair或其他完善转发函数; they are not designed to be used that way. 它们并非设计用于这种方式。

First, I would be careful about constness, since you're taking the address of the reference, but expecting the pair to be a pointer to a non-constant tree::branch. 首先,由于要获取引用的地址,因此我要注意常量性,但是希望该对是指向非常量tree :: branch的指针。

If you have lambdas, I would do the following. 如果您有lambda,我将执行以下操作。

std::transform(step.first->tree.begin()
    , step.first->tree.end()
    , std::back_inserter(steps)
    [&](tree::branch& branch) { return std::make_pair(&branch, new_item); }
);

If you don't have lambda support, it might be clearer if you attempt to write the functor yourself, at least in the short term, to get clearer errors. 如果您没有lambda支持,则至少在短期内尝试自己编写函子可能会更清楚,从而获得更清晰的错误。 boost::bind is powerful, but when it fails the error messages can sometimes hide something fundamental. boost::bind功能强大,但是当失败时,错误消息有时会隐藏一些基本信息。

struct make_step_info
{ 
   HTREEITEM new_item;

   make_step_info(HTREEITEM new_item): new_item(new_item)  {};

   std::pair<tree::branch*,HTREEITEM> operator()(tree::branch& branch) const
   {
      return std::make_pair(&branch, new_item);
   }
}

/* And use it here */
std::transform(step.first->tree.begin()
     , step.first->tree.end()
     , std::ba_ck_inserter(steps)
     make_step_info(new_item)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM