std::any 用于无法复制构造的对象 [英] std::any for objects that can't be copy constructed

查看:36
本文介绍了std::any 用于无法复制构造的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 unique_ptr 的对象,因此如果不进行深度复制(我不想要),就无法复制构造.

I have an object which holds a unique_ptr and as such can't be copy constructed without doing a deep copy (which I don't want).

我想让 std::any 持有那个对象,但我发现的唯一替代方法是让 std::any 持有一个指针,这会增加一个无用的间接,或者让我的对象有一个唯一的点.下面的代码有望说明我的观点:

I'd like to have std::any hold that object, but the only alternatives I've found is to either make std::any hold a pointer, which adds a useless indirection, or make my object have a unique ptr. The code bellow will hopefully illustrate my point:

//Compiled with clang++ -std=c++2a; clang version 5.0.0
#include <iostream>
#include <any>
#include <memory>

struct A {
        std::unique_ptr<int> m = std::make_unique<int>(11);
        A(A&& a): m(std::move(a.m)) {}
        A() {}
};

struct B {
        std::shared_ptr<int> m = std::make_shared<int>(11);
};


template<class T>
void use_any_ptr() {
        std::any a{new T{}};
        std::cout << *(std::any_cast<T*>(a)->m) << std::endl;
}

template<class T>
void use_any() {
        std::any a{T{}};
        std::cout << *(std::any_cast<T>(a).m) << std::endl;
}


int main() {
        use_any_ptr<A>(); // Workaround using a pointer
        use_any<B>(); // Workaround using shared pointer
        use_any<A>(); // Breaks because A has no cc no matching constructor for initialization of 'std::any'
}

据我所知,std::any 的构造似乎需要复制对象,但是我不确定为什么不能简单地移动对象.

As far as I understand the construction of std::any seems to require copying of the object, however I'm unsure why the object couldn't simply be moved.

有没有办法解决这个问题?也就是说,除了使用 shared_ptr 之外,这意味着我基本上是为了创建 any 对象或传递 std::any 一个指针(这是一个不需要的间接级别,因为 std::据我所知,any 持有一个指向要使用的类型的空指针).

Is there a way to work around this ? That is, other than using a shared_ptr, which means I'm basically expressing the "wrong thing" for the sake of creating the any object or passing std::any a pointer (which is an unneeded level of indirection, since std::any holds a void pointer to the type to being with as far as I can tell).

是否有任何不同的实现可以在创建时使用移动 ctr 而不是复制 ctr?

Is there a different implementation of any that could use the move ctr at creation instead of the copy ctr ?

还是我很傻,不理解这里的真正"问题?

Or am I being silly and not understanding the "real" problem here ?

推荐答案

还是我很傻,不理解这里的真正"问题?

Or am I being silly and not understanding the "real" problem here ?

问题在这里很简单:std::any复制构造函数,因此它要求包含的对象是可复制构造的.

The problem is simple here: std::any has a copy constructor and thus it requires the object contained is copy constructible.

您可以解决它的方法是定义一个包含指针的自定义对象,该对象是可复制的,并为其实现正确逻辑(无论正确意味着什么在您的情况下,我看不到您如何复制 std::any 以及其中包含的拥有 std::unique_ptr 的对象,但也许您可以在你的情况下以某种方式摆脱它).

The way you can work around it is by defining a custom object that contains the pointer, that is copyable and that implements a proper logic for it (whatever proper means in your case, I can't see how you could copy an std::any and thus an object contained there that owns an std::unique_ptr, but maybe you can get away with it somehow in your case).

否则重新考虑您的设计并尝试摆脱对 std::any 的需求.它确实没有太多用途,也许你可以通过一些模板机制或 std::variant 或其他任何东西获得相同的效果

Otherwise rethink your design and try to get rid of the need of an std::any. It hasn't much uses indeed and probably you can get the same with a bit of template machinery or an std::variant or whatever

这篇关于std::any 用于无法复制构造的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆