gpt4 book ai didi

c++ - 如何根据模板类型启用继承开关

转载 作者:行者123 更新时间:2023-11-30 05:13:44 24 4
gpt4 key购买 nike

我想根据传递给类的模板参数实现一个实现开关:

  • 如果传递的模板类型派生自特定类(此处:Serializable) 然后创建一个容器 DataElement该类型的实例应派生自 SerializableElement 和重载从它继承的两个纯虚方法(这里:unloadTo 和 loadFrom)。
  • 然而,如果传递的模板类型不是从 Serializable 派生的DataElement 不应派生自 SerializableElement,因此不应重载纯虚方法。

我调整了我的代码,以便能够通过此处提到的技巧将类方法隐藏在 DataElement 类中 C++: Enable method based on boolean template parameter

不过,在针对我的问题调整技巧时,我确实遇到了以下错误:

error: cannot declare variable ‘dataEle’ to be of abstract type ‘DataElement’


testEnableIF.h

#ifndef TESTENABLEIF_H_
#define TESTENABLEIF_H_


#include <iostream>
#include <type_traits>

/// @brief some dummy class to mark a type as Serializable
class Serializable {};

/// @brief abstract base class which adds additional abilities to DataElement
class SerializableElement {
public:
virtual void unloadTo(int x) =0;
virtual void loadFrom(int x) =0;
};
/// @brief used by the DataElement class
class DisableSerializability {};

/// @brief is a container class which manages the creation and
/// access to a data element of type _DataType
template< typename _DataType, bool _IsDataSerializable = std::is_base_of<Serializable,_DataType>::value >
class DataElement :
// derive from SerializableElement, only if the template type _DataType is derived from the interface Serializable
public
std::conditional<
_IsDataSerializable,
SerializableElement, // add additional properties to DataElement if its Serializable
DisableSerializability >::type {
public:
DataElement(): m_content(new _DataType()){}

void foo(int x) { std::cout << "in foo" << std::endl; }

template <bool _EnabledAbility = _IsDataSerializable>
void unloadTo(typename std::enable_if<_EnabledAbility, int>::type x)
{ std::cout << "in unloadTo" << std::endl; }

template <bool _EnabledAbility = _IsDataSerializable>
void loadFrom(typename std::enable_if<_EnabledAbility, int>::type x)
{ std::cout << "in loadFrom" << std::endl; }

private:
_DataType* m_content;
};


#endif /* TESTENABLEIF_H_ */

调用DataElement类的测试代码

main.cpp

#include "testEnableIF.h"

class SerializableType : public Serializable {
int x;
int y;
int z;
};
class NonSerializableType {
int u;
};

int main() {
SerializableType sType;
NonSerializableType nType; // other type without being derived from Serializables

DataElement<SerializableType> dataEle;
dataEle.unloadTo(3);

return 0;
}

最佳答案

template <bool _EnabledAbility = _IsDataSerializable>
void unloadTo(typename std::enable_if<_EnabledAbility, int>::type x);

不是覆盖

virtual void unloadTo(int x) = 0;

您甚至可以通过添加 override 来获得错误。

修复的方法是从继承自 SerializableElement 的具体类继承,而不是仅继承接口(interface)。

Demo

关于c++ - 如何根据模板类型启用继承开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43802189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com