我刚刚重新启动了一个暂停了几个月的项目.上次我编译它它工作得很好,没有任何错误或警告.然而,当我今天早些时候尝试编译它时,我得到了这个警告
attention : ‘template<class _Operation> class std::binder2nd’ is deprecated [-Wdeprecated-declarations]
Run Code Online (Sandbox Code Playgroud)
这个警告字面上出现了数百次,包括我在项目中使用的Eigen/Geometry
In file included from [...]/include/Eigen/src/Core/ArrayBase.h:109:0,
from [...]/include/Eigen/Core:350,
from [...]/include/Eigen/Geometry:4,
from [...]/include/[myproject]/types.hh:8,
from [...]/include/[myproject]/voronoi.hh:8
Run Code Online (Sandbox Code Playgroud)
从那以后我没有更新Eigen(使用3.2.4,这仍然是今天的最后一次更新).但是,自从我上次编译它以来,GCC已经更新到5.1.0(我使用的是archlinux)
题:
我推测这std::bind2nd是非常弃用的,并且已经完成了提交以解决在Eigen中的问题.然而,这个提交尚未与主分支合并:/(并没有解决问题,因为一些std::bind2nd仍存在于Eigen的代码中)
底线是:不推荐使用Eigen的最后一个稳定版本
我有一个foo是std::vector<int>.它表示一组范围的"边缘"值.
例如,如果foo是{1,3,5,7,11},那么范围是1-3,3-5,5-7,7-11.对我而言,这相当于4个时期.请注意,每个句点包括范围中的第一个数字,而不是最后一个数字.所以在我的例子中,8出现在第3个(从零开始)的时期.7也出现在第3期.11以上不会出现在任何地方.2出现在第0期.
鉴于bar哪个是int,我使用
std::find_if(
foo.begin(),
foo.end(),
std::bind2nd(std::greater<int>(), bar)
) - foo().begin() - 1;
Run Code Online (Sandbox Code Playgroud)
给我应该包含的时期bar.
我的问题:std::bind2nd已被弃用,所以我应该重构.使用更新函数的等效语句是什么?std::bind不会以明显的方式"堕入".
在调用func()这段代码时,我最近花了很多时间来理解错误消息:
int main()
{
vector< vector<double> > v;
double sum = 0;
for_each( v.begin(), v.end(),
bind2nd( ptr_fun(func), &sum ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
什么时候func()这样声明,代码编译得很好:
void func( vector<double> v, double *sum )
{
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个声明(为了效率),我得到一个编译器错误:
void func( const vector<double> &v, double *sum )
{
}
Run Code Online (Sandbox Code Playgroud)
我期望看到的错误类似于引用引用错误,因为binder2nd的operator()的定义,
result_type operator()(const argument_type& _Left) const
Run Code Online (Sandbox Code Playgroud)
相反,令我惊讶的是,Visual C++(VS2012)编译器给我的错误是:
错误C2535:'void std :: binder2nd <_Fn2> :: operator()(const std :: vector <_Ty>&)const':已定义或声明的成员函数
我无法解读.
operator()是已经定义?我得到的完整错误是:
error C2535: 'void std::binder2nd<_Fn2>::operator …Run Code Online (Sandbox Code Playgroud) 我缺少的是部分应用函数的第二个参数而不是第一个参数的能力.当我想将函数传递给map之类的东西时,这尤其有用,但每次都不必为它编写lambda.
我为此编写了我自己的函数(下面定义,以防万一没有任何内置函数,其他任何人都很好奇),但我真的想知道Prelude中是否已存在某些内容因为我更喜欢重用而不是重新发明.
这是我的定义和一个简单的例子:
bind2nd :: (a -> b -> c) -> b -> a -> c
bind2nd f b = \a -> f a b
foo :: Int -> Bool -> String
foo n b | b = show n
| otherwise = "blabla"
alwaysN :: Int -> String
alwaysN = bind2nd foo True
Run Code Online (Sandbox Code Playgroud) 如何使用binder2nd,bind2nd和bind1st?更具体地说何时使用它们并且它们是否必要?另外,我正在寻找一些例子.
我遇到了这段代码.从输出时,除以2,但语法不熟悉我,我可以推断,其余阵列存储阵列数量的剩余部分.
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ( )
{
int numbers[ ] = {1, 2, 3};
int remainders[3];
transform ( numbers, numbers + 3, remainders, bind2nd(modulus<int>( ), 2) );
for (int i = 0; i < 3; i++)
{
cout << (remainders[i] == 1 ? "odd" : "even") << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
变换和bind2nd在这种情况下做了什么?我阅读了文档,但我不清楚.