如何使用std::ref
How to use std::ref?
使用std::ref
的正确方法是什么?我在VS2010中尝试了以下代码,但它没有编译:
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
using namespace std;
struct IsEven
{
bool operator()(int n)
{
if(n % 2 == 0)
{
evens.push_back(n);
return false;
}
return true;
}
vector<int> evens;
};
int main(int argc, char **argv)
{
vector<int> v;
for(int i = 0; i < 10; ++i)
{
v.push_back(i);
}
IsEven f;
vector<int>::iterator newEnd = remove_if(v.begin(), v.end(), std::ref(f));
return 0;
}
错误:
c: \program files(x86)\微软visual studio10.0\vc\include\xxresult(28):错误C2903:"result":符号既不是类模板,也不是函数模板
c: \program files(x86)\微软visual studio10.0\vc\include\xxresult(28):错误C2143:语法错误:缺少";"在'<'之前
再加上一些。。。
在std::ref
的Visual C++10.0实现中存在一个或多个错误。
据报道,它已经为Visual C++11修复;看看我之前的问题。
微软的STL如此回答:"我们已经修复了它,该修复将在VC11 RTM中提供。(然而,该修复没有进入VC11测试版。)"
我在VS2010中收到了相同的编译错误,并通过从std::unary_function
:继承进行了更正
struct IsEven : std::unary_function<int, bool>
我只是考虑到result
出现在错误消息中。我只能猜测,在VS2010中,std::ref
取决于unary_function
:中的typedef
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type;
typedef Result result_type;
};
编辑:
请参阅Cheers和hth-中的答案Alf关于VS2010中的错误。
相关文章:
- 为什么我的 std::ref 无法按预期工作?
- std::bind 是否实现了 std::ref 和 std::cref 来消除函数调用的歧义?
- 将对象传递给函数而不将其包装到 std::ref 中,而参数被指定为 const 引用
- 带有自定义deleter的std::unique_ptr对象的大小(一个由ref捕获的lambda)
- 当请求的键丢失时,有没有办法从向量映射返回 std::vector ref?
- By-ref 参数:这是 std::thread 和 std::bind 之间的不一致吗?
- 无法通过 std::ref() 使用 auto& 参数调用 std::invoke()
- How does c++11 std::ref work?
- 扩展 std::thread 的 ref 对象的传递范围
- 使用std :: async时,为什么通过const ref速度较慢
- 从gcc 5.4升级至gcc 6.3 std::thread std:ref issue
- Create Eigen::Ref from std::vector
- C 11:STD Ref Global变量和非功能 - 局部线程界初始化订单
- 如何 SWIG std::string& to C# ref string
- Clang bug with std::function, std::bind and std::ref?
- 在 GCC 4.x / C++11 中是否对 std::string ref 计数
- 这是 std::ref 行为合乎逻辑吗?
- 调用函数,该函数是 std::bind(ed) 与 std::ref 参数
- 在向线程传递引用时,std::ref真的是必要的吗
- std::tie 和 std::make_tuple 带有 std::ref 参数有什么区别