我想将一个rvalue传递std::bind
给一个在C++ 0x中采用rvalue引用的函数.我无法弄清楚该怎么做.例如:
#include <utility>
#include <functional>
template<class Type>
void foo(Type &&value)
{
Type new_object = std::forward<Type>(value); // move-construct if possible
}
class Movable
{
public:
Movable(Movable &&) = default;
Movable &operator=(Movable &&) = default;
};
int main()
{
auto f = std::bind(foo<Movable>, Movable());
f(); // error, but want the same effect as foo(Movable())
}
Run Code Online (Sandbox Code Playgroud) 我个人实验之一,了解一些C++ 0x特性:我正在尝试将函数指针传递给模板函数来执行.最终执行应该发生在不同的线程中.但是对于所有不同类型的函数,我无法使模板工作.
#include <functional>
int foo(void) {return 2;}
class bar {
public:
int operator() (void) {return 4;};
int something(int a) {return a;};
};
template <class C>
int func(C&& c)
{
//typedef typename std::result_of< C() >::type result_type;
typedef typename std::conditional<
std::is_pointer< C >::value,
std::result_of< C() >::type,
std::conditional<
std::is_object< C >::value,
std::result_of< typename C::operator() >::type,
void>
>::type result_type;
result_type result = c();
return result;
}
int main(int argc, char* argv[])
{
// call with a function pointer
func(foo);
// call with a …
Run Code Online (Sandbox Code Playgroud)