相关疑难解决方法(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++ 1y没有从std :: bind到std :: function的可行转换

我试图存储一个前向功能std::function.如果我使用std::bind,我收到错误消息no viable conversion from ....如果我使用lambda,它编译好了.

这是示例代码

#include <functional>

template<typename Handler>void func1(int a, Handler&& handler) {}
template<typename Handler>void func2(Handler&& handler)
{
    // this line compile fine
    std::function<void ()> funcA = [handler = std::move(handler)]() { func1(1, std::move(handler)); };
    // this line got compile error
    std::function<void ()> funcB = std::bind(func1<Handler>, 1, std::move(handler));
}

int main()
{
    func2(&main); // this just a sample, I am using functor as argument in real code
}
Run Code Online (Sandbox Code Playgroud)

尝试g ++ --std = …

c++ c++11 c++14

20
推荐指数
1
解决办法
4397
查看次数

标签 统计

c++ ×2

c++11 ×2

c++14 ×1

rvalue ×1

rvalue-reference ×1

std ×1