move-semantics

    1热度

    1回答

    有没有写一个拷贝构造一类(比方说,Copyable,即持有std::unique_ptr到Base类(但一个真正的方式存储Derived对象。 快速测试显示了预期的切片时,因为Copyable不知道它的控股真正的类型,所以我想需要一个clone方法,但我不知道是否有办法让编译器在一些更好的方式来处理这个 纵切代码:? #include <algorithm> #include <iostream

    1热度

    2回答

    所以我在下面的文件,其内容如下: 啊: #ifndef A_H #define A_H class A { }; #endif BH: #ifndef B_H #define B_H #include <memory> class A; class B { public: B(std::unique_ptr<A> a); private: st

    -1热度

    1回答

    我最近看了这个std::thread参考。 对于移动构造函数,它说: thread(thread&& other); 移动的构造。构造线程对象来表示由 其他表示的执行线程。 此次调用后其他不再代表 执行的线程。 此外,在下面的例子中有这些行: int n=0; std::thread t3(f2, n); std::thread t4(std::move(t3)); // t4 is no

    1热度

    2回答

    这是我的理解,当一个基本的数据类型被移出时,它将始终执行一个副本。 例如,你永远无法从int移动,并有原始int没有指定值: #include <iostream> int main() { int x = 100; int y{std::move(x)}; std::cout << x << " " << y; //always prints "100 100

    1热度

    1回答

    是否有一种干净的方式来移动一套集合中的元素?例如: set<A> s {...}; vector<A> v; for (const A& a : s) v.push_back(move(const_cast<A&>(a))); s.clear(); //undefined behavior?

    5热度

    1回答

    在阅读关于C++ 11时,我有一种感觉,当使用标准容器(如std::vector)和用户定义的数据类型时,我们鼓励提供noexcept移动操作(如果有的话),因为随后 - 只 - 然后容器会内部真正移动数据而不是复制。 当今天尝试了这个,我发现与-std=c++1y(对于C++ 14)和g ++ - 4.8甚至没有区别。也许我错过了规范中的更新,也许我的例子是错误的。 我比较了三种数据结构,应该是

    3热度

    2回答

    我想构建一个可调用的对象链,以后可以异步执行。我想尝试以下方法:构建节点的“嵌套”结构(通过将每个节点移动到其“父”中)导致存储所有计算的对象,并且可以按需启动链。 这是我脑子里想的: template <typename TParent, typename TF> struct node { TParent _parent; TF _f; node(TPare

    3热度

    4回答

    假设验证码: class Parent {} class Child : public Parent {} static std::vector<std::unique_ptr<Child>> Foo(); 有没有写这个功能的一个简单的方法: std::vector<std::unique_ptr<Parent>> Bar() { auto children = Foo();

    7热度

    3回答

    假设有这样的功能,移动: int * func() { std::unique_ptr<int> ptr(new int(3)); //Few more lines of code //then one function added where programmer writes like some thing SOME_OTHER_FUNC(std:

    4热度

    3回答

    在我的API中,我有一个返回std::istringstream的函数。 std::istringstream class是不可复制的,但支持移动一个符合标准的编译器,返回本地std::istringstream没有问题。 但是,在gcc 4.9上,有no support用于移动std::istringstream。 有没有一些解决方法,我可以使用该std::istringstream而不从用户的