c++ - 使用 std::bind 时出现运行时错误

标签 c++ c++11 visual-studio-2013 stdbind

以下代码使用 Xcode 6.3.2 在 OS X 上编译并成功运行。但是,当使用 VS2013 在 Windows 上运行时它会崩溃。

#include <vector>
#include <string>
#include <memory>
#include <functional>
#include <iostream>

void validate(const std::string& name, 
              int value, 
              std::vector<std::string>& values)
{
    std::cout << "name: " << name << " value: " << value << "\n";
}

struct Property
{
public:
    Property(const std::string& name,
             const std::function<void(const std::string&, int)>& validationFunction) :
        m_name(name),
        m_validationFunction(validationFunction)
    {
    }

    const std::string m_name;
    const std::function<void(const std::string&, int)> m_validationFunction;
};


const std::vector<std::shared_ptr<Property>> properties
{
    {
        std::make_shared<Property>("identifier1",
        [](const std::string& name, int value)
        {
            validate(name, value, std::vector<std::string>{"foo"});
        })
    },

    {
        std::make_shared<Property>("identifier2",
                                   std::bind(validate,
                                             std::placeholders::_1,
                                             std::placeholders::_2,
                                             std::vector<std::string>{"bar"}))
    }
};


int main()
{
    properties[0]->m_validationFunction(properties[0]->m_name, 4);
    properties[1]->m_validationFunction(properties[1]->m_name, 5);

    return 0;
}

崩溃的原因是 properties 中的第一个元素似乎已损坏。当我检查内存时,我看到了这个:

properties  { size=2 }  std::vector<std::shared_ptr<Property>,std::allocator<std::shared_ptr<Property> > >
[size]  2   int
[capacity]  2   int
[0] shared_ptr {m_name=<Error reading characters of string.> m_validationFunction={...} } [4277075694 strong refs, 4277075693 weak refs] [{_Uses=4277075694 _Weaks=4277075694 }]    std::shared_ptr<Property>
[1] shared_ptr {m_name="identifier2" m_validationFunction=bind(0x011315a5 {schema-init.exe!validate(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &)}, (_1, _2, { size=1 })) } [1 strong ref] [make_shared]    std::shared_ptr<Property>
[Raw View]  0x0115295c {schema-init.exe!std::vector<std::shared_ptr<Property>,std::allocator<std::shared_ptr<Property> > > properties} {...}    std::vector<std::shared_ptr<Property>,std::allocator<std::shared_ptr<Property> > > *

如果我将 std::bind 替换为直接调用 validate 作为 properties 中的第一个元素,那么代码将成功执行。

谁能解释一下我做错了什么。

最佳答案

我注意到 Paul R 上面的评论说他使用 VC2013 成功运行了代码,所以我更新了我的 Visual Studio 2013 拷贝(更新 5)并且代码现在运行正常。所以我猜它一个编译器错误。

关于c++ - 使用 std::bind 时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35938666/

相关文章:

c++ - 如何在头文件中定义具有继承性的结构?

c++ - DirectX 设备大写字母

c++ - 如何在 C++ 中为 sqlite 进行非静态回调?

c++ - boost lambda 或 phoenix 问题:使用 std::for_each 对容器的每个元素进行操作

c++ - JavaCV 中的密集光流 (DualTVL1)

c++ - NvAPI NVAPI_INTERFACE 缺少显式类型

c++ - 在 C++ Windows 应用程序中引用 native C++ DLL 仅在添加为引用时有效

visual-studio - 使用智能感知和 Vsvim

c++ - 如何使用空终止字符memset char数组?

c++ - 将 export 关键字与模板一起使用