【发布时间】:2018-06-06 14:41:23
【问题描述】:
尝试为模板化成员结构添加 std::iterator_traits 时出现错误 - 即,我有一个迭代器类,它是模板化外部类的成员:
namespace Toolbox {
template <typename CharType>
class substring_container_adapter
{
public:
struct iterator // : public std::iterator<std::forward_iterator_tag, const CharType *> C++ 17 is very upset at this approach!
{
// iterator constructor
iterator(const CharType * pszPosition, const CharType * pszDelimeters)
稍后,我尝试将迭代器特征的部分特化添加到 std,因为显然不推荐从 std::iterator 继承(尽管 - 或 boost::iterator_adaptor 非常有意义并且实际上在这种情况和其他情况下都有效)...
// define iterator traits for our custom iterators
namespace std
{
template <typename CharType>
struct iterator_traits<class Toolbox::substring_container_adapter<CharType>::iterator>
{
using iterator_category = forward_iterator_tag;
using value_type = CharType;
};
}
但是,VC++ 2017 版本 15.7.3(为此项目启用了 C++ 17)抱怨:
错误 C2764:“CharType”:模板参数未在部分特化“std::iterator_traits::iterator>”中使用或推导出来
为什么不呢?
我怀疑这是 !@#$ 烦人的限制,因为试图部分专门化成员结构而不是 substring_container_adapter 之外的模板化结构?
【问题讨论】:
-
partial specialization 我想你只是错过了将
class关键字添加到struct iterator_traits<class Toolbox:: ...中 -
而且你根本不需要那些部分模板专业化:) 因为ForwardIterator
-
FWIW - 添加更多
typename说明符不会改变事情(在 VS 2017 中) - 相同的错误结果。似乎基本缺乏语言,它无法处理模板内部类的部分规范构造(但我希望有人可以在这里澄清这一点) -
@VictorGubin 我不知道这有什么帮助(目前)——因为概念仍然不是语言的一部分。
-
请注意,您需要所有 5 个别名都是正确的
std::iterator_traits,因此您不会在那里获得任何东西。
标签: visual-studio templates c++17 partial-specialization