decltypeauto.cpp

The following code example is taken from the book
C++17 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2017
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License


#include <iostream>

template<decltype(auto) N>
struct S {
  void printN() const {
    std::cout << "N: " << N << '\n';
  }
};

static const int c = 42;
static int v = 42;

int main()
{
  S<c> s1;      // deduces N as const int 42
  S<(c)> s2;    // deduces N as const int& referring to c
  s1.printN();
  s2.printN();
  
  S<(v)> s3;    // deduces N as int& referring to v
  v = 77;
  s3.printN();  // prints: N: 77
}