相关疑难解决方法(0)

通过std :: bind传递rvalues

我想将一个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++ std rvalue rvalue-reference c++11

27
推荐指数
1
解决办法
1万
查看次数

模板,函数指针和C++ 0x

我个人实验之一,了解一些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)

c++ templates function-pointers c++11

9
推荐指数
1
解决办法
1444
查看次数