0

I want to decltype the return type by the param 'c.data()'. Could you give me some ideas.

my env: gcc4.8.5,c11.

template <class T>
auto data(const T& c)
  -> std::enable_if<std::is_convertible<decltype(c.data()), const char*>::type, const char*>::type {
  return c.data();
}
lo chan
  • 1
  • 1

1 Answers1

0

You need to use typename to tell the compiler that std::enable_if<>::type is a type:

template <class T>
auto data(const T& c)
  -> typename std::enable_if<std::is_convertible<decltype(c.data()), const char*>::type, const char*>::type {
  return c.data();
}

It's worth noting that P0634 makes typename optional in C++20, which means your code is well-formed in C++20.

康桓瑋
  • 33,481
  • 5
  • 40
  • 90