4 ms·
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
by tonetheman 3y 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();
}
}
- sitkack 3y agoNot 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.
- Leherenn 3y agoHow do you call it and what is your error message?
- secondcoming 3y agoLike this maybe? https://godbolt.org/z/6fnhT6h9v https://godbolt.org/z/6fnhT6h9v
- tonetheman 3y agoYeah 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.
- secondcoming 3y agoIt should work in C++17 too, the compile settings are just left over from something else I was doing on CE.
- Thorrez 3y agostd::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 https://en.cppreference.com/w/cpp/concepts/integral
- mmebane 3y agoIn this case, I think it's a typo, and should be std::is_integral: https://en.cppreference.com/w/cpp/types/is_integral https://en.cppreference.com/w/cpp/types/is_integral