Hacker News new | ask | show | jobs
by tonetheman 1122 days ago
What compiler is he/she using? I cannot get this to compile at all but I might not know the magic compiler incantation to get it to work.

  template<typename T>
  auto length(const T& value) noexcept {
      if constexpr (std::integral<T>::value) { // is number
          return value;
      }
      else {
          return value.length();
      }
  }
4 comments

Not related to your post, but it was marked dead, then I looked at your comment history and didn't notice anything, but many of your comments were marked dead. I "vouched" for this one. It looks like you angered someone and they have been flagging all of your posts.
Yeah I was using godbolt too and wondered why I could not get it to compile. Just funny the article is about c++17 but you need c++20 on there to get it to compile.

My c++ is super old. But thanks that is great I could not figure it out.

It should work in C++17 too, the compile settings are just left over from something else I was doing on CE.
std::integral was added in C++20 , so this example will only work in C++20. Weird that the author used this example.

https://en.cppreference.com/w/cpp/concepts/integral

In this case, I think it's a typo, and should be std::is_integral:

https://en.cppreference.com/w/cpp/types/is_integral

How do you call it and what is your error message?