4

C++17 introduce helper variable template:

template <typename T>
inline constexpr bool is_integral_v = is_integral<T>::value;

But I see a lot people miss inline key word when they define their own traits, Is this equivalent to following or not?

template <typename T>
constexpr bool is_integral_v = is_integral<T>::value;
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • constexpr variables are inline implicitly, so there's no need to use constexpr inline – Oblivion Aug 29 '19 at 08:39
  • Possible duplicate of [How do inline variables work?](https://stackoverflow.com/questions/38043442/how-do-inline-variables-work) In particular, https://stackoverflow.com/a/53896763 – L. F. Aug 29 '19 at 09:13
  • Basically, the variant *with* `inline` is for preventing ODR violations. If none of your code would trigger an ODR violation, it's not needed, but it's safer to add it nonetheless (just in case). More info : [Should `const` and `constexpr` variables in headers be `inline` to prevent ODR violations?](https://stackoverflow.com/questions/53794625/should-const-and-constexpr-variables-in-headers-be-inline-to-prevent-odr-v) – Sander De Dycker Aug 29 '19 at 09:14
  • @SanderDeDycker That's actually a better dupe target. – L. F. Aug 29 '19 at 09:16
  • 5
    @Oblivion constexpr variables are **not** inline implicitly, only constexpr static member variables are inline implicitly. see [cppreference](https://en.cppreference.com/w/cpp/language/constexpr). – 康桓瑋 Aug 29 '19 at 09:51

0 Answers0