Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

New rules for auto deduction from braced-init-list

Use case

Cleaner initialization.

Explanation

auto x{value} now deduces to the value’s type, not std::initializer_list.

Code

#include <iostream>

int main() {
  // C++17: deduces int.
  auto addr0{0x1000};

  // Before C+17: it would deduce std::initializer_list<int>
  // auto {42};

  // std::initializer_list still works with =.
  auto addr1 = {0x1100, 0x1200, 0x1300};

  std::cout << "addr0: 0x" << std::hex << addr0 << "\n";
  std::cout << "addr1.size: " << addr1.size() << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++17/build/new-rules-for-auto-deduction-from-braced-init-list
addr0: 0x1000
addr1.size: 3