运算符的模板参数特化问题!=

时间:2013-09-05 18:08:32

标签: c++ templates

我在我编写的模板稀疏矩阵类YaleStorage<D>上有一个STL样式的迭代器。我为矩阵类编写了operator==,它允许比较两个矩阵D可能不同。

我的迭代器也是模板 - 因此我可以将const_iteratoriterator作为iterator_T<RefType>的特化,其中RefType可以是const D&或{ {1}}。

即使D&不同,我也需要这些迭代器具有可比性。我并不特别需要constness来区分(D不需要与const_iterator进行比较)。以下是我编写iterator等内容的方法:

operator!=

请注意,我编写了自己的template <typename D> class YaleStorage { template <typename RefType> class iterator_T { template <typename E, typename ERefType = typename enable_if_else<std::is_const<RefType>::value, const E&, E&>::type> bool operator!=(const typename YaleStorage<E>::template stored_diagonal_iterator_T<ERefType>& rhs) const { /* some comparison code */ } }; typedef iterator_T<D&> iterator; typedef iterator_T<const D&> const_iterator; const_iterator cbegin() const { /* returns a const_iterator */ } const_iterator cend() const { /* returns the end const_iterator */ } }; ,因为我在STL中找不到类似的功能。这是:

enable_if_else

不幸的是,当我尝试进行常规迭代器比较(template <bool B, typename TA, typename TB> struct enable_if_else {}; template <typename TA, typename TB> struct enable_if_else<true, TA, TB> { typedef TA type; }; template <typename TA, typename TB> struct enable_if_else<false, TA, TB> { typedef TB type; }; )时,我遇到了大量错误,例如:

D=E

我已经尝试明确地投射compiling ../../../../ext/nmatrix/storage/yale.cpp ../../../../ext/nmatrix/storage/yale.cpp: In instantiation of ‘VALUE nm::yale_storage::each_stored_with_indices(VALUE) [with DType = unsigned char; VALUE = long unsigned int]’: ../../../../ext/nmatrix/storage/yale.cpp:1686:3: required from here ../../../../ext/nmatrix/storage/yale.cpp:1567:83: error: no match for ‘operator!=’ in ‘d != nm::YaleStorage<D>::cend() const [with D = unsigned char; nm::YaleStorage<D>::const_iterator = nm::YaleStorage<unsigned char>::iterator_T<const unsigned char&>]()’ ../../../../ext/nmatrix/storage/yale.cpp:1567:83: note: candidates are: In file included from ../../../../ext/nmatrix/storage/yale.cpp:64:0: ../../../../ext/nmatrix/storage/yale.h:438:10: note: template<class E, class ERefType> bool nm::YaleStorage<D>::iterator_T::operator!=(const typename nm::YaleStorage<E>::iterator_T<ERefType>&) const [with E = E; ERefType = ERefType; RefType = const unsigned char&; D = unsigned char] ../../../../ext/nmatrix/storage/yale.h:438:10: note: template argument deduction/substitution failed: ../../../../ext/nmatrix/storage/yale.cpp:1567:83: note: couldn't deduce template parameter ‘E’ 的结果,所以它知道比较的两边都有什么,但我不明白为什么它会扼杀这个。

cend()

我知道如何为非template <typename DType> static VALUE each_with_indices(VALUE nm) { YaleStorage<DType> y(s); // The next line is 1567: for (typename YaleStorage<DType>::const_iterator d = y.cbegin(); d != y.cend()); ++d) { /* Do some stuff with d */ } } 函数明确指定模板参数,但我该如何为运算符执行此操作?还有另一个更好的方法吗?在我看来,编译器应该明白operator在这里是什么。

0 个答案:

没有答案