Tuesday, October 10, 2017

C++17: New Rules for auto Deduction

Auto deduction for direct-list initialization must have only a single element. Here is an example:

int main()
{
  //auto v = {9, 10}; //Should be good, but error in MSVS 2017:
                      //  Copy-list-initialization. decltype(y) is
                      //  std::initializer_list.
  //auto w = {1, 2.0};//Error: Copy-list-initialization and types of 
                      //  elements of braced-init-list are not identical.
    auto x{3};        //Good: Direct list-initilization and single element.
                      //  decltype(x) is int.
  //auto y     = {3}; //Should be good, but error in MSVS 2017:
                      //  Copy-list-initialization. decltype(y) is
                      //  std::initializer_list.
  //auto z{3, 4};     //Error: Direct list-initialization and multiple
                      //  elements.
  return 0;
}
References:
https://en.wikipedia.org/wiki/C%2B%2B17
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3922.html

No comments:

Post a Comment