C++ Function bind repeating arguments to curried function

c++, currying, gcc

Solution

Copying strings is expensive. Since `std::bind` thinks that the values of the placeholders are only used once, it performs a `std::move` on the strings. This is done for each Parameter and as a consequence, either `b` or `c` is a moved, that means empty string.

You can change that behavior by explicitly saying what you mean, by passing the arguments by const-reference:

string concatthreestrings(string const& a,string const& b,string const& c)

Now, it should work.

Problem

I am trying to understand the concept of currying and calling a function which concats three strings but by passing only two strings and using the second argument twice. However when I do this, the second argument is not getting sent to the function at all and it prints out an empty string. Is it some really obvious mistake? ``` string concatthreestrings(string a,string b,string c){ cout<<"Value of A: "<<a<<endl; cout<<"Value of B: "<<b<<endl; cout<<"Value of C: "<<c<<endl; return a+b+c; } int main() { typedef std::function< string( string,string) > fun_t ; using namespace std::placeholders; fun_t fn = std::bind( concatthreestrings, _1, _2, _2); cout<<endl<<fn( "First","Second")<<endl; } ``` This is giving the below output. Doesnt using the _2 twice mean that second argument be passed for both second and third. If a use a string in its place its working fine.

Original source

Related problems