C++ C++;带有指针的类模板专门化
我有以下格式的树结构:C++ C++;带有指针的类模板专门化,c++,class,templates,pointers,C++,Class,Templates,Pointers,我有以下格式的树结构: template <typename DataType> class Tree { DataType *accessData() { return data; } Tree *child1, *child2; DataType *data; }; template <typename DataType> class Root : public Tree<DataType> { // root pr
template <typename DataType>
class Tree {
DataType *accessData() { return data; }
Tree *child1, *child2;
DataType *data;
};
template <typename DataType>
class Root : public Tree<DataType> {
// root provides storage of nodes; when it goes out of scope, the
// entire tree becomes invalid
MemoryPool<Tree> nodeStorage;
MemoryPool<DataType> dataStorage;
};
现在我当然可以只保留当前模板,但我不喜欢这种额外的指针访问,特别是需要在根目录中分配int。关于如何专门化模板以提供此功能或其他方法,有什么想法吗
我尝试过像这样专门化模板(还有无数其他方法)
模板树{…}
但我总是会遇到编译错误。任何帮助都将不胜感激 我建议使用可以用作
数据类型
模板参数的相同接口定义多个数据
类。从数据访问的方式中抽象数据的存储方式
template<typename T>
class value_data
{
private:
T _value;
public:
T& access() { return _value; }
const T& access() const { return _value; }
};
template<typename T>
class unique_ptr_data
{
private:
std::unique_ptr<T> _value;
public:
T& access() { assert(_value != nullptr); return *_value; }
const T& access() const { assert(_value != nullptr); return *_value; }
};
enum class my_enum { /* ... */ };
class my_enum_data
{
private:
my_enum _value;
public:
my_enum& access() { return _value; }
const my_enum& access() const { return _value; }
};
我建议使用可以用作
数据类型
模板参数的相同接口定义多个数据
类。从数据访问的方式中抽象数据的存储方式
template<typename T>
class value_data
{
private:
T _value;
public:
T& access() { return _value; }
const T& access() const { return _value; }
};
template<typename T>
class unique_ptr_data
{
private:
std::unique_ptr<T> _value;
public:
T& access() { assert(_value != nullptr); return *_value; }
const T& access() const { assert(_value != nullptr); return *_value; }
};
enum class my_enum { /* ... */ };
class my_enum_data
{
private:
my_enum _value;
public:
my_enum& access() { return _value; }
const my_enum& access() const { return _value; }
};
我建议使用traits类来推断
树中存储的对象类型
// The default traits.
template <typename DataType> struct TreeDataType
{
using Type = DataType*;
};
template <typename DataType>
class Tree {
// Define the data type using the traits class.
using Data = typename TreeDataType<DataType>::Type;
Data accessData() { return data; }
Tree *child1, *child2;
Data data;
};
我建议使用traits类来推断树中存储的对象类型
// The default traits.
template <typename DataType> struct TreeDataType
{
using Type = DataType*;
};
template <typename DataType>
class Tree {
// Define the data type using the traits class.
using Data = typename TreeDataType<DataType>::Type;
Data accessData() { return data; }
Tree *child1, *child2;
Data data;
};
非常有趣地使用了auto
,我将阅读您的完整答案,谢谢!非常有趣地使用了auto
,我将阅读您的完整答案,谢谢!这看起来真的干净又短。方法名称没有区别,但这只是一个很小的否定。谢谢!在我看来,这是最好的解决办法。所需要的专业化是琐碎的,我真的很喜欢清洁。谢谢@bombax,我很高兴能帮上忙。祝你好运。这看起来真的干净又短。方法名称没有区别,但这只是一个很小的否定。谢谢!在我看来,这是最好的解决办法。所需要的专业化是琐碎的,我真的很喜欢清洁。谢谢@bombax,我很高兴能帮上忙。祝你好运
// The default traits.
template <typename DataType> struct TreeDataType
{
using Type = DataType*;
};
template <typename DataType>
class Tree {
// Define the data type using the traits class.
using Data = typename TreeDataType<DataType>::Type;
Data accessData() { return data; }
Tree *child1, *child2;
Data data;
};
template <> struct TreeDataType<MyEnum>
{
using Type = MyEnum;
};