如何解决本身就是模板的 class 模板的类型定义内部 class?
How to address a typedefed inner class of a class template that itself is a template?
我有一个句法问题,
在向量 class、
内使用以下迭代器 class
#include <memory>
#include <cstddef>
#include <iterator>
namespace ft { template <class T, class Alloc = std::allocator<T> > class vector; }
template <class T, class Alloc>
class ft::vector
{
public:
typedef T value_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::pointer pointer;
typedef ptrdiff_t difference_type;
template <class value_type>
class ptr_iterator : public std::iterator<std::random_access_iterator_tag, value_type>
{
public:
ptr_iterator();
};
typedef ptr_iterator<value_type> iterator;
typedef ptr_iterator<const value_type> const_iterator;
};
如何在声明之外寻址 ptr_iterator
的构造函数?
我这样试过:
template <class T, class Alloc>
ft::vector<T, Alloc>::iterator::iterator() {}
但它产生了
Vector.hpp:120:1: error: specializing member 'ft::vector<T, Alloc>::ptr_iterator<T>::iterator' requires 'template<>' syntax
ft::vector<T, Alloc>::iterator::iterator() {}
我尝试在两个 iterator
单词后添加 <> 并将第二个 iterator
单词替换为 ptr_iterator,
但它仍然无法编译。
如果不重复函数签名上方的两个模板,我还能使用 typedef 吗?
或者是否有更好的迭代器声明设计,允许我同时定义迭代器和 const_iterator?
对于外联定义,很遗憾,您必须重复所有嵌套模板的整个“签名”,包括约束(如果有的话)。 Cigien 给出了正确的形式:
template <class T, class Alloc>
template <class value_type>
ft::vector<T, Alloc>::ptr_iterator<value_type>::ptr_iterator()
{}
由于这可能很混乱且容易出错,您还可以定义嵌套的 class 内联,或者定义为非嵌套的助手 class。在 libstdc++ 中,前一种方法用于视图的迭代器,而后者用于像 std::vector
、for example:
这样的容器
typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
const_iterator;
编辑:
天哪,我不确定是否可以定义这样的运算符!因为它是一个自由函数,所以不需要嵌套的模板参数,我会把它放在 ft
命名空间中。它必须看起来像这样:
namespace ft {
template <class T, class Alloc, class value_type>
bool operator==(
typename vector<T, Alloc>::template ptr_iterator<value_type> const& lhs,
typename vector<T, Alloc>::template ptr_iterator<value_type> const& rhs)
{ return true; }
}
但是,我已经在 Compiler Explorer 上试过这段代码,我的编译器抱怨说它无法推断出向量 class.
的参数
我有一个句法问题,
在向量 class、
内使用以下迭代器 class#include <memory>
#include <cstddef>
#include <iterator>
namespace ft { template <class T, class Alloc = std::allocator<T> > class vector; }
template <class T, class Alloc>
class ft::vector
{
public:
typedef T value_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::pointer pointer;
typedef ptrdiff_t difference_type;
template <class value_type>
class ptr_iterator : public std::iterator<std::random_access_iterator_tag, value_type>
{
public:
ptr_iterator();
};
typedef ptr_iterator<value_type> iterator;
typedef ptr_iterator<const value_type> const_iterator;
};
如何在声明之外寻址 ptr_iterator
的构造函数?
我这样试过:
template <class T, class Alloc>
ft::vector<T, Alloc>::iterator::iterator() {}
但它产生了
Vector.hpp:120:1: error: specializing member 'ft::vector<T, Alloc>::ptr_iterator<T>::iterator' requires 'template<>' syntax
ft::vector<T, Alloc>::iterator::iterator() {}
我尝试在两个 iterator
单词后添加 <> 并将第二个 iterator
单词替换为 ptr_iterator,
但它仍然无法编译。
如果不重复函数签名上方的两个模板,我还能使用 typedef 吗?
或者是否有更好的迭代器声明设计,允许我同时定义迭代器和 const_iterator?
对于外联定义,很遗憾,您必须重复所有嵌套模板的整个“签名”,包括约束(如果有的话)。 Cigien 给出了正确的形式:
template <class T, class Alloc>
template <class value_type>
ft::vector<T, Alloc>::ptr_iterator<value_type>::ptr_iterator()
{}
由于这可能很混乱且容易出错,您还可以定义嵌套的 class 内联,或者定义为非嵌套的助手 class。在 libstdc++ 中,前一种方法用于视图的迭代器,而后者用于像 std::vector
、for example:
typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator;
typedef __gnu_cxx::__normal_iterator<const_pointer, vector>
const_iterator;
编辑:
天哪,我不确定是否可以定义这样的运算符!因为它是一个自由函数,所以不需要嵌套的模板参数,我会把它放在 ft
命名空间中。它必须看起来像这样:
namespace ft {
template <class T, class Alloc, class value_type>
bool operator==(
typename vector<T, Alloc>::template ptr_iterator<value_type> const& lhs,
typename vector<T, Alloc>::template ptr_iterator<value_type> const& rhs)
{ return true; }
}
但是,我已经在 Compiler Explorer 上试过这段代码,我的编译器抱怨说它无法推断出向量 class.
的参数