1

Simply put, I'd like a compile-time check to ensure that a specific overload of a function exists. Either way the compilation will fail but I want a more useful error message to explain what needs to be fixed, as well as trying to reduce compiler spam as it tries to convert T into every known overload of other_serialize_func.

class Foo { /*....*/ };

std::string other_serialize_func(const Foo&) {}; // this function may or may not actually exist

template <typename T>
class Bar {
  std::string serialize() {
    static_assert(/*WHAT TO PUT HERE*/, "Type must have other_serialize_func(const T&) overload");
    return other_serialize_func(T);
  }
};
ryan0270
  • 1,095
  • 9
  • 29
  • https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-template-to-check-for-a-functions-existence may help. – Zalman Stern Dec 14 '17 at 20:28
  • You'll get the same error message unless you put the return statement in an `if constexpr`, and that won't look pretty. As for how, search for detection idiom and you'll see countless examples – Passer By Dec 14 '17 at 20:31
  • @Passer, can you elaborate? – ryan0270 Dec 14 '17 at 20:32
  • @ZalmanStern, I saw that question too but my attempts to convert that answer to my case(free function with specific overload) are failing – ryan0270 Dec 14 '17 at 20:42
  • @Jesper, sorry my title was misleading (change the details as I started to write out the question). I'll edit to avoid that confusion. But also, to_string has to be manually defined for, e.g. custom classes – ryan0270 Dec 14 '17 at 20:44

0 Answers0