c++14 SFINAE dereference iterator

Source: Internet
Author: User

c++14 SFINAE dereference iterator

Original problem: write function f (r), if R is an iterator, then return F (*r), otherwise return R.

summary: questions:
  • What is an iterator?

    • iterators are a concept in C + +, and it is an iterator type if type it meets the following conditions

      • can be copied construction ( Copyconstructible)

      • can copy assignment ( Copyassignable)

      • can be destructor ( Destructibale)

      • left value It Object exchangeable (swappable)

      • Std::iterator_traits<it> includes the following types of Members:value_type, difference_type, reference, pointer and ITERATOR_ Category

      • for An lvalue R for It, with the following expression being legal and having the specified meaning:

          • *r return value type: Unspecified Preconditions: r

          • ++r < Span style= "font-size:small" > return value type: It & preconditions: r self-increment

in subsequent implementations, the requirements for iterators are relaxed: R with Lvalue R, or if *r is legal, then there is an iterator type. Meaning That:

write the function F (r), if the Lvalue R can be dereferenced, return F (*r), otherwise return R.

problem Analysis:

The return value type of f changes with the actual parameter. If R is an int * * type, A minimum of two F overloads of int *f (int * *) and int f (int *) are required. These function overloads need to be generated by a call to f at compile time through generics .

when called, the overload resolution stage requires the compiler to select the appropriate function Template.

    • t The lvalue can be dereferenced, Select the function template ( 1

       template <class  t>auto f (T r) {
       Span style= "color: #0000ff" >return  f (*r);}  
    • Otherwise select a function template ( 2)
       template <class  t>auto f (T r) { 
      return   
question:
    • < Span lang= "zh-cn" > How do I select a function template based on the above rules?

      • f templates are valid, Select the template ( 1 Span style= "font-family:noto Sans CJK SC Regular" >

      • legally defined as: r can be dereferenced.

questions:
    • How do I express a priority selection ?

      • by overloading an implicit conversion sequence Hierarchy. Sothat the function generated by the template (1) is graded higher than the template (2)

    • < Span lang= "zh-cn" > How to express legitimacy?

      • sfinae r< Span style= "font-family:noto Sans CJK SC Regular" > cannot be dereferenced, making the template ( 1 (substitution failure)

implementation:
1#include <cassert>2 3#include <type_traits>4#include <utility>5 6Template <classT>7 Auto F (T x, ...) {8   returnx;9 }Ten  oneTemplate <classTclass= Decltype (*std::d eclval<t> ()) > aAuto F (T x,int) { -   returnF (*x, 0); - } the  - intmain () { -   intx =3, *p = &x; -Assert (f (&p,0)==3); +    -   return 0; +}

text:

Withheld

FAQ:

Not Yet.

references:

Http://en.cppreference.com/w/cpp/concept/Iterator

Http://en.cppreference.com/w/cpp/language/overload_resolution (return Type Deduction)

Http://en.cppreference.com/w/cpp/utility/declval

Http://en.cppreference.com/w/cpp/language/overload_resolution

Http://en.cppreference.com/w/cpp/language/template_argument_deduction

c++14 SFINAE dereference iterator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.