所以,我有一些类似于这些结构的东西:
struct Generic {}
struct Specific : Generic {}
在某些时候,我需要向下投掷,即:
Specific s = (Specific) GetGenericData();
这是一个问题,因为我收到错误消息,指出没有可用的用户定义的转换。
我可以将代码更改为:
Specific s = (*(Specific *)&GetGenericData())
或使用reinterpret_cast,它将是:
Specific s = *reinterpret_cast<Specific *>(&GetGenericData());
但是,有没有办法让它更干净? 也许使用宏或模板?
我查看了这篇文章C++协变模板,我认为它有一些相似之处,但不确定如何为我的情况重写它。 我真的不想将事物定义为SmartPtr。 我宁愿保持事物的本来面目。
看起来您的用法中的GetGenericData()
返回一个Generic
的按值,在这种情况下,由于对象切片,强制转换为Specific
将不安全。
要做你想做的事情,你应该让它返回一个指针或引用:
Generic* GetGenericData();
Generic& GetGenericDataRef();
然后你可以表演一个演员表:
// safe, returns nullptr if it's not actually a Specific*
auto safe = dynamic_cast<Specific*>(GetGenericData());
// for references, this will throw std::bad_cast
// if you try the wrong type
auto& safe_ref = dynamic_cast<Specific&>(GetGenericDataRef());
// unsafe, undefined behavior if it's the wrong type,
// but faster if it is
auto unsafe = static_cast<Specific*>(GetGenericData());
我在这里假设你的数据很简单。
struct Generic {
int x=0;
int y=0;
};
struct Specific:Generic{
int z=0;
explicit Specific(Generic const&o):Generic(o){}
// boilerplate, some may not be needed, but good habit:
Specific()=default;
Specific(Specific const&)=default;
Specific(Specific &&)=default;
Specific& operator=(Specific const&)=default;
Specific& operator=(Specific &&)=default;
};
鲍勃是你的叔叔。 int z
默认初始值设定项有点重要,因此我们不必在父级 ctor 中重复它。
我明确了 thr ctor,因此只会显式调用它,而不是偶然调用。
这是简单数据的合适解决方案。
所以第一步是意识到你有一个动态状态问题。 存储状态的性质会根据动态信息而变化。
struct GenericState { virtual ~GenericState() {} }; // data in here
struct Generic;
template<class D>
struct GenericBase {
D& self() { return *static_cast<D&>(*this); }
D const& self() const { return *static_cast<D&>(*this); }
// code to interact with GenericState here via self().pImpl
// if you have `virtual` behavior, have a non-virtual method forward to
// a `virtual` method in GenericState.
};
struct Generic:GenericBase<Generic> {
// ctors go here, creates a GenericState in the pImpl below, or whatever
~GenericState() {} // not virtual
private:
friend struct GenericBase<Generic>;
std::unique_ptr<GenericState> pImpl;
};
struct SpecificState : GenericState {
// specific stuff in here, including possible virtual method overrides
};
struct Specific : GenericBase<Specific> {
// different ctors, creates a SpecificState in a pImpl
// upcast operators:
operator Generic() && { /* move pImpl into return value */ }
operator Generic() const& { /* copy pImpl into return value */ }
private:
friend struct GenericBase<Specific>;
std::unique_ptr<SpecificState> pImpl;
};
如果您希望能够复制,请在 GenericState
中实现 virtual GenericState* clone() const
方法,并在SpecificState
协变覆盖它。
我在这里所做的是正则化类型(如果我们不支持移动,则半正则化)。 "特定"和"泛型"类型不相关,但它们的后端实现详细信息(泛型状态和"特定状态")是相关的。
主要通过 CRTP 和 GenericBase
避免接口重复。
现在,向下转换可以涉及动态检查,也可以不涉及动态检查。 你穿过pImpl
并把它扔掉。 如果在右值上下文中完成,它会移动 - 如果在左值上下文中,它会复制。
如果您愿意,可以使用共享指针而不是唯一指针。 这将允许基于非复制非移动的铸造。
好的,经过一些额外的研究,我想知道这样做是否有什么问题:
struct Generic {}
struct Specific : Generic {
Specific( const Generic &obj ) : Generic(obj) {}
}
如果我错了,请纠正我,但这使用隐式复制构造函数有效。
假设是这种情况,我可以避免编写一个并自动执行转换,现在我可以编写:
Specific s = GetGenericData();
当然,对于大型对象,这可能不是一个好主意,但对于较小的对象,这将是一个"正确"的解决方案吗?