Gri*_*ngo 8 c++ templates compiler-errors bind2nd
在调用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 ()(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.
Run Code Online (Sandbox Code Playgroud)
这种行为是明确定义的(每个正确的C++编译器都无法编译您的代码).
从D.9.3类模板的标准(N3376)部分来看binder2nd,operator()存在以下两种定义:
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;
Run Code Online (Sandbox Code Playgroud)
如果first_argument_type已经是const T&,那么他们将是冲突的.
| 归档时间: |
|
| 查看次数: |
5196 次 |
| 最近记录: |