mercredi 13 janvier 2021

what the line of stl source code(integral_constant part) is for?

  /// integral_constant
  template<typename _Tp, _Tp __v>
    struct integral_constant
    {
      static constexpr _Tp                  value = __v;
      typedef _Tp                           value_type;
      typedef integral_constant<_Tp, __v>   type;
      constexpr operator value_type() const noexcept { return value; }
#if __cplusplus > 201103L

#define __cpp_lib_integral_constant_callable 201304

      constexpr value_type operator()() const noexcept { return value; }
#endif
    };

  template<typename _Tp, _Tp __v>
    constexpr _Tp integral_constant<_Tp, __v>::value;

I can't understand what function of this line

constexpr _Tp integral_constant<_Tp, __v>::value;

And why there is no error when it declare in global as follows:

struct test
{
  static const int a = 1;
};
const int test::a;

Aucun commentaire:

Enregistrer un commentaire