【问题标题】:std::bind - compiling unexpected (undeclared identifier) error in visual studiostd::bind - 在 Visual Studio 中编译意外(未声明的标识符)错误
【发布时间】:2013-03-12 11:09:22
【问题描述】:

对我来说,另一个令人不安的错误消息,如果我正确理解 std::bind 我可以像 _1 这样的参数来定义一个未给定的参数?正确的?考虑以下行:

std::function<bool(value_type, const std::string &)> 
                             func(std::bind(&Pred, _1, "name"));

这应该可行,对吧?这将用于 std::find_if() 函数,因此第一个参数应该是值类型,第二个参数应该是字符串。

但是,Visual Studio 2010 对此进行了抱怨,并显示以下错误消息:

错误 C2065:“_1”:未声明的标识符

这很奇怪,我怎么能在 Visual Studio 中说“嘿,第一个参数没有绑定”。 Pred 是一个简单的函数,以 value_type, const std::string&amp; 作为参数 - 返回一个布尔值。

【问题讨论】:

  • std::placeholders::_1

标签: c++ visual-studio-2010 c++11 stdbind


【解决方案1】:

在你的情况下,你想要这个:

std::function<bool(value_type, const std::string &)> 
                         func(std::bind(&Pred, std::placeholders::_1, "name"));

【讨论】:

  • 嗯,这实际上是有道理的...... en.cppreference.com/w/cpp/utility/functional/bind 把我放在错误的脚上:P
  • @paul23 是的,页面不是很清楚占位符位于不同的命名空间中 - 我只是编辑了页面以使其更明显:-)
猜你喜欢
  • 2014-08-17
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 2022-03-09
  • 2020-05-09
  • 1970-01-01
相关资源
最近更新 更多