小编Tom*_*Tom的帖子

为什么我不能用{std :: move(first),std :: move(second)}实例化std :: vector <std :: unique_ptr <int >>?

我有一个简单的函数,它应该构造一些对象并返回它们的向量,同时还转移所有权.我认为这样做的最好方法就是返回一个std::vector<std::unique_ptr<int>>对象(让我们说它们就是这样int).

当我尝试以下功能时:

std::vector<std::unique_ptr<int>> create_stuff() {
    auto first = std::make_unique<int>(1);
    auto second = std::make_unique<int>(2);
    return {std::move(first), std::move(second)};
}
Run Code Online (Sandbox Code Playgroud)

我受到了很长的编译错误的欢迎,结果是:

xmemory0(737): error C2280: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)':
attempting to reference a deleted function
Run Code Online (Sandbox Code Playgroud)

我认为问题在于函数本身,但是以下解决方案工作正常:

std::vector<std::unique_ptr<int>> create_stuff() {
    auto first = std::make_unique<int>(1);
    auto second = std::make_unique<int>(2);
    std::vector<std::unique_ptr<int>> results;
    results.push_back(std::move(first));
    results.push_back(std::move(second));
    return results;
}
Run Code Online (Sandbox Code Playgroud)

为什么第二种解决方案有效但不是第一种?有没有一种解决方法可以让我在初始化列表中使用简短的语法?

c++ initializer-list unique-ptr

10
推荐指数
1
解决办法
151
查看次数

将带有unique_ptr的可变lambda传递给const&std :: function

我有一个调度函数,它在主线程中执行给定的lambda.为了这个问题,假设它看起来如下:

void dispatch(const std::function<void()>& fn) {
    fn();
}
Run Code Online (Sandbox Code Playgroud)

我需要在新线程中加载一个新对象而不会中断主线程.所以我执行以下操作:1)启动一个新线程并在线程内创建一个新的唯一指针,2)调用dispatch并传播它所属的新唯一指针.

std::unique_ptr<std::string> foo; // nullptr

// do the loading in a new thread:
std::thread t([&](){
    // in the new thread, load new value "Blah" and store it temporarily
    auto bar = std::make_unique<std::string>("Blah");
    dispatch([bar2 = std::move(bar), &foo]() mutable {
        foo = std::move(bar2); // propagate the loaded value to foo
    });
});
t.join(); // for the sake of this example

std::cout << "foo = " << *foo << std::endl; // this should …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++14

7
推荐指数
1
解决办法
398
查看次数

应用建模矩阵查看矩阵=失败

我在OpenGL中移动和旋转对象时遇到了问题.我正在使用C#和OpenTK(Mono),但我想问题是我不理解OpenGL部分,所以即使你对C#/ OpenTK一无所知,你也可以帮助我.

我正在阅读OpenGL SuperBible(最新版),我试图用C#重写GLFrame.这是我已经重写的部分:

public class GameObject
{
    protected Vector3 vLocation;
    public Vector3 vUp;
    protected Vector3 vForward;

    public GameObject(float x, float y, float z)
    {
        vLocation = new Vector3(x, y, z);
        vUp = Vector3.UnitY;
        vForward = Vector3.UnitZ;
    }

    public Matrix4 GetMatrix(bool rotationOnly = false)
    {
        Matrix4 matrix;

        Vector3 vXAxis;
        Vector3.Cross(ref vUp, ref vForward, out vXAxis);

        matrix = new Matrix4();
        matrix.Row0 = new Vector4(vXAxis.X, vUp.X, vForward.X, vLocation.X);
        matrix.Row1 = new Vector4(vXAxis.Y, vUp.Y, vForward.Y, vLocation.Y);
        matrix.Row2 = new Vector4(vXAxis.Z, vUp.Z, …
Run Code Online (Sandbox Code Playgroud)

c# opengl 3d opentk

3
推荐指数
1
解决办法
1563
查看次数

标签 统计

c++ ×2

3d ×1

c# ×1

c++14 ×1

initializer-list ×1

lambda ×1

opengl ×1

opentk ×1

unique-ptr ×1