Im using a simple SFINAE trick to check if a member function exists, for example:
#include <type_traits> template <typename C> struct has_size { template <typename T> static constexpr auto check(T*) -> decltype(std::declval<T const>().size(), std::true_type{}); template <typename> static constexpr auto check(...) -> std::false_type; static constexpr bool value = decltype(check<C>(nullptr))::value; }; // Usage: static_assert(has_size<std::vector<int>>::value, "std::vector<int> has size()");
(Im aware that theres a simpler method now, but didn't come back when I wrote this piece of code.)
This code works on GCC. However, Clang issues warning 1 (all versions prior to Apple LLVM 7.3, depending on whatโs at the top):
decltype.cpp:15:27: error: inline function 'has_size<std::__1::vector<int, std::__1::allocator<int> > >::check<std::__1::vector<int, std::__1::allocator<int> > >' is not defined [-Werror,-Wundefined-inline] static constexpr auto check(T*) -> ^ decltype.cpp:22:44: note: used here static constexpr bool value = decltype(check<C>(nullptr))::value;
In other words, clang expects functions to be defined, not just declared, even if they are never called (only in an invaluable decltype context).
Is this a bug in clang? Or is it a right to complain? If so, is GCC correct when accepting this code?
Also, when writing this question, I realized that clang compilation errors can be avoided altogether by removing the constexpr qualifier before the member function template. What changes the presence of constexpr ?
1 What is the problem for compiling Im using -Werror . There are some warnings that are based on heuristics and therefore have unavoidable false positives, but this is not as far as I can see.