Tuesday, October 3, 2017

C++14: std::integral_constant gained an operator()

C++14 added the member function operator() to integral_constant. Integral_constant wraps a static constant. operator() returns the constants value. Here is an example:

#include <iostream>
#include <type_traits>
using namespace std;

integral_constant<long long, 42ll> Answer;

int main()
{
  cout << Answer()     << " ";
  cout << Answer.value << " ";
  cout <<        integral_constant<long long, 42ll>::value_type(Answer) << " ";
  cout <<        integral_constant<long long,  0ll>::value_type(Answer) << " ";
  cout << sizeof(integral_constant<long long, 42ll>::value_type)        << " ";
  cout << sizeof(integral_constant<long long, 42ll>::type      )        << " ";

  cout << endl;
  return 0;
}
// Output: 42 42 42 42 8 1
Reference: http://www.cplusplus.com/reference/type_traits/integral_constant/

No comments:

Post a Comment