Monday, September 25, 2017

C++14: Variable templates

C++14 allows variables to be templated. Here is an example:

#include <iostream>

template<typename T>
constexpr T npos = T(-1); 

int main()
{
  std::cout << npos<         double   > << " ";
  std::cout << npos<         long long> << " ";
  std::cout << npos<         int      > << " ";
  std::cout << npos<         short    > << " ";
  std::cout << npos<unsigned long long> << " ";
  std::cout << npos<unsigned int      > << " ";
  std::cout << npos<unsigned short    > << " ";
  std::cout << std::endl;
  return 0;
}
// Output: -1 -1 -1 -1 18446744073709551615 4294967295 65535
Reference: https://en.wikipedia.org/wiki/C%2B%2B14#Variable_templates

No comments:

Post a Comment