std::vector 的模板推导指南:qualified/non-qualified 其他标准类型的名称

Template deduction guide for std::vector: qualified/non-qualified names for other std types

来自cppreference:s deduction guides for std::vector,提供以下推导指南:

template< class InputIt,
          class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
vector(InputIt, InputIt, Alloc = Alloc())
  -> vector<typename std::iterator_traits<InputIt>::value_type, Alloc>;

哪里(在discussion of the page) vector does not include its namespace qualifier, which I interpret follows from clauses 1 and 3 from 17.10 (temp.deduct.guide)中也提到了:

1 ... Deduction guides are not found by name lookup.

3 ... A deduction-guide shall be declared in the same scope as the corresponding class template and, for a member class template, with the same access.

我想知道限定名称是否用于例如模板参数化中的 std::allocator 和 simple-template-id 中的 std::iterator_traits 是绝对必要的。从上面3的引用部分,推导指南不应该像vector那样在namespace std内声明吗?

问题

演绎指南没有特殊的名称查找规则,因此如果非限定查找可以找到名称,则不需要使用限定名称。事实上,standard itself中的推导指南使用了不合格的名称:

namespace std {

  // [...]

  template<class InputIterator,
           class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
    vector(InputIterator, InputIterator, Allocator = Allocator())
      -> vector<typename iterator_traits<InputIterator>::value_type, Allocator>;

  // [...]

}

cppreference 的做法是在允许的情况下使用限定名称,但这是一种文体选择,可以提高表示的清晰度、减少歧义并与我们的链接基础结构很好地配合使用。


注意 deduction guides 的语法需要在 ( 之前有一个 template-name 和一个 simple -template-id-> 之后。两者都不允许资格。不合格名称的另一个常见示例是当我们想要引用 class(模板)的注入-class-名称时。