Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weird compiler error using bind2nd(): "member function already defined or declared" instead of "reference to reference"

I recently spent quite some time understanding the error message when calling func() in this piece of code:

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:

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,

result_type operator()(const argument_type& _Left) const

Instead, to my surprise, the error the Visual C++ (VS2012) compiler gave me was:

error C2535: 'void std::binder2nd<_Fn2>::operator ()(const std::vector<_Ty> &) const' : member function already defined or declared

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.
like image 227
Grim Fandango Avatar asked Sep 10 '12 10:09

Grim Fandango


1 Answers

This behavior is well-defined (every correct C++ compiler will fail to compile your code).

From the standard (N3376) section D.9.3 on class template binder2nd, these two defintions of operator() exist:

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.

like image 134
Travis Gockel Avatar answered Nov 05 '22 05:11

Travis Gockel