I can typedef
the return type of a std::bind
expression, but apparently the compiler is not happy with me calling the default-constructor of this type. Instead, I have to pass the exact same arguments that I did to std::bind
in the first place (cf below); why? Am I doing this wrong?
#include <iostream>
#include <functional>
#define x_plus_one(T) std::bind<T>( f_plus<T>(), std::placeholders::_1, T(1) )
template <class T>
struct f_plus
{
inline T operator() ( const T& a, const T& b ) const
{ return a + b; }
};
template <class T>
using x_plus_one_type = decltype( x_plus_one(T) );
int main()
{
auto f = x_plus_one(double);
std::cout<< f(3.2) << "\n";
// auto g = x_plus_one_type<double>(); ==> fails, no matching constructor
auto g = x_plus_one_type<double>( f_plus<double>(), std::placeholders::_1, 1 ); // but this works?!
std::cout<< g(3.2) << "\n";
}
I also tried passing an f_minus
function object, and swapping the placeholder and 1
, but the compiler doesn't like it, which really confuses me. The only little "hack" that worked was replacing 1
which 2
, which tells me that whatever the type returned by bind
is, it doesn't copy the inputs. Why not? Is there a way to force the copy?
Type returned by std::bind
is unspecified. As such, it is unspecified if it can be constructed in any way other than by calling std::bind
. You can use std::unique_ptr
or boost::optional
to have an unconstructed value.