c++ - 另一个模板(同一类)的模板特化

标签 c++ templates template-specialization specialization

我正在编写一个数组类。这个数组类可以再次包含数组作为成员。在实现打印功能时,我需要专门化。

26:template <class T> class array : public vector<T>{
public:
    ...
       string* printToString();
    ...
};
...           
template <class T> string* array<T>::printToString(){
   ...  // generic function
}
template <> inline string* array<double>::printToString(){
   ...  // spezialization for double, works
}
561:template <class U> string* array<array<U>*>::printToString(){
   ...  // does not work
}

最后的定义产生

src/core/array.h:561: error: invalid use of incomplete type ‘class array<array<T> >’
src/core/array.h:26: error: declaration of ‘class array<array<T> >’

如果重要的话,g++ 版本是 g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3。 有什么想法吗?

提前致谢, 托马斯

最佳答案

作为 David 解决方案的替代方案,您可以无条件地将调用转发给一组重载函数:

template <class T> class array;
namespace details {
  template <class T> std::string array_print(array<T> const&);
  std::string array_print(array<double> const&); // Regular function 
  template <class T> std::string array_print(array<array<T> > const&);
}

template <class T> class array : private vector<T> {
public:
    ...
       std::string printToString() { return details::array_print(*this); }
    ...
};

namespace details { /* implementions after class is defined */ }

关于c++ - 另一个模板(同一类)的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7268901/

相关文章:

c++ - 对以下 c/c++ 解决方案的逻辑解释

c++ - CONCEPT_REQUIRES_ ranges-v3 中的实现

c++ - 从 C# 切换到 C++。我的代码有什么问题?我需要标题来完成我想做的事情吗?一个文件问题中的类定义

c++ - 是否可以在 Visual C++ Build Tools 2015 Update 3 上应用修复程序

c++ - 优雅地切换一组函数的模板参数

c++ - 我可以单独定义类模板的函数模板成员吗?

templates - 用于 CSV 导入的 Libreoffice Calc 模板

c++ - 比较两个类型的多重集是否相等

c++ - 模板化派生类的特化函数模板

C++ 模板部分特化 : Why cant I match the last type in variadic-template?