假设我有
struct foo {
void ham() {}
void ham() const {}
};
struct bar {
void ham() {}
};
假设我有一个模板化函数,我能否判断给定类型是否具有 ham
的 const 重载?
最佳答案
与
#define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \
template <typename U> \
class traitsName \
{ \
private: \
template<typename T, T> struct helper; \
template<typename T> \
static std::uint8_t check(helper<signature, &funcName>*); \
template<typename T> static std::uint16_t check(...); \
public: \
static \
constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t); \
}
DEFINE_HAS_SIGNATURE(has_ham_const, T::ham, void (T::*)() const);
然后
static_assert(has_ham_const<foo>::value, "unexpected");
static_assert(!has_ham_const<bar>::value, "unexpected");
关于c++ - 如何检查成员函数是否有 const 重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348561/