I recently spent quite some time understanding the error message when calling func()
in this piece of code:
我最近花了相当多的时间来理解在这段代码中调用func()时的错误消息:
int main()
{
vector< vector<double> > v;
double sum = 0;
for_each( v.begin(), v.end(),
bind2nd( ptr_fun(func), &sum ) );
return 0;
}
when func()
was declared like so, the code compiled fine:
当func()这样声明时,代码编译良好:
void func( vector<double> v, double *sum )
{
}
when I used this declaration (for efficiency), I got a compiler error:
当我使用这个声明时(为了提高效率),我得到了一个编译错误:
void func( const vector<double> &v, double *sum )
{
}
The error I expected to see was something like a reference-to-reference error, because of the definition of operator() of binder2nd,
我期望看到的错误类似于引用到引用的错误,因为binder2的算子()的定义,
result_type operator()(const argument_type& _Left) const
Instead, to my surprise, the error the Visual C++ (VS2012) compiler gave me was:
相反,令我惊讶的是,Visual c++ (VS2012)编译器给我的错误是:
error C2535: 'void std::binder2nd<_Fn2>::operator ()(const std::vector<_Ty> &) const' : member function already defined or declared
错误C2535: 'void std::binder2 <_Fn2>:::operator ()(const std:::vector<_Ty> &) const':成员函数已经定义或声明
which I cannot decipher.
我无法破译。
- Can you explain under which mechanism the
operator()
is already defined? - 您能解释一下操作符()是在什么机制下定义的吗?
The full error I got was:
我得到的全部错误是:
error C2535: 'void std::binder2nd<_Fn2>::operator ()(const std::vector<_Ty> &) const' : member function already defined or declared
with
[
_Fn2=std::pointer_to_binary_function<const std::vector<double> &,double *,void,void (__cdecl *)(const std::vector<double> &,double *)>,
_Ty=double
]
c:\vc\include\xfunctional(319) : see declaration of 'std::binder2nd<_Fn2>::operator ()'
with
[
_Fn2=std::pointer_to_binary_function<const std::vector<double> &,double *,void,void (__cdecl *)(const std::vector<double> &,double *)>
]
c:\consoleapplication1.cpp(31) : see reference to class template instantiation 'std::binder2nd<_Fn2>' being compiled
with
[
_Fn2=std::pointer_to_binary_function<const std::vector<double> &,double *,void,void (__cdecl *)(const std::vector<double> &,double *)>
]
Build FAILED.
2 个解决方案
#1
6
This behavior is well-defined (every correct C++ compiler will fail to compile your code).
这种行为是定义良好的(每个正确的c++编译器都不能编译代码)。
From the standard (N3376) section D.9.3
on class template binder2nd
, these two defintions of operator()
exist:
从class template binder2的standard (N3376)节D.9.3中可以看出,算子()有两个定义:
typename Fn::result_type
operator()(const typename Fn::first_argument_type& x) const;
typename Fn::result_type
operator()(typename Fn::first_argument_type& x) const;
If first_argument_type
is already a const T&
, then they will be conflicting.
如果first_argument_type已经是const &,那么它们将相互冲突。
#2
0
This isn't an answer, but I just want to record the modern C++11 solution, where all the small bind helpers are deprecated in favour of the universal std::bind
:
这并不是一个答案,但我只想记录现代的c++ 11解决方案,在这个解决方案中,所有的小绑定助手都被弃用,取而代之的是通用的std::bind: bind:
#include <functional>
#include <vector>
#include <algorithm>
void func(std::vector<double> const & v, double * sum) { /* ... */ }
int main()
{
std::vector<std::vector<double>> v;
double sum = 0;
std::for_each(v.begin(), v.end(), std::bind(func, std::placeholders::_1, &sum));
}
The variadic templates of C++11, as well as a more comprehensive collection of type-modifying traits, give std::bind
much stronger deduction abilities than the previous components in <functional>
.
c++ 11的变量模板,以及更全面的类型修改特性集合,使std::绑定比之前
#1
6
This behavior is well-defined (every correct C++ compiler will fail to compile your code).
这种行为是定义良好的(每个正确的c++编译器都不能编译代码)。
From the standard (N3376) section D.9.3
on class template binder2nd
, these two defintions of operator()
exist:
从class template binder2的standard (N3376)节D.9.3中可以看出,算子()有两个定义:
typename Fn::result_type
operator()(const typename Fn::first_argument_type& x) const;
typename Fn::result_type
operator()(typename Fn::first_argument_type& x) const;
If first_argument_type
is already a const T&
, then they will be conflicting.
如果first_argument_type已经是const &,那么它们将相互冲突。
#2
0
This isn't an answer, but I just want to record the modern C++11 solution, where all the small bind helpers are deprecated in favour of the universal std::bind
:
这并不是一个答案,但我只想记录现代的c++ 11解决方案,在这个解决方案中,所有的小绑定助手都被弃用,取而代之的是通用的std::bind: bind:
#include <functional>
#include <vector>
#include <algorithm>
void func(std::vector<double> const & v, double * sum) { /* ... */ }
int main()
{
std::vector<std::vector<double>> v;
double sum = 0;
std::for_each(v.begin(), v.end(), std::bind(func, std::placeholders::_1, &sum));
}
The variadic templates of C++11, as well as a more comprehensive collection of type-modifying traits, give std::bind
much stronger deduction abilities than the previous components in <functional>
.
c++ 11的变量模板,以及更全面的类型修改特性集合,使std::绑定比之前