简体   繁体   中英

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

From cppreference:s deduction guides for std::vector , the following deduction guide is presented:

 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>; 

Where (as also mentioned in the 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.

I wonder about whether the qualified names, used for eg std::allocator in the template parametrization, and for std::iterator_traits in the simple-template-id, are strictly necessary. From the quoted part of 3 above, mustn't the deduction guide be declared within namespace std , as vector is?

Question(s)

  • Is it necessary to use qualified names as in the deduction guide for std::vector above, when these should be available via unqualified lookup (same scope as the deduction guide)?
  • If "no" , what would be the foremost motivation to use the qualified name(s)?

There's no special name lookup rule for deduction guides, and therefore no requirement to use qualified names if unqualified lookup would find the name. In fact, the deduction guide in the standard itself uses unqualified names:

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>;

  // [...]

}

The practice of cppreference is to use qualified names whenever allowed, but this is a stylistic choice to improve the clarity of presentation, minimize ambiguities, and play nicely with our linking infrastructure.


Note that the grammar of deduction guides requires a template-name before the ( and a simple-template-id after the -> . Neither permits qualification. Another common example of unqualified names is when we want to refer to the injected-class-name of a class (template).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM