【发布时间】:2017-11-29 03:03:35
【问题描述】:
我现在尝试学习 SFINAE,但似乎我遇到了强制问题,我该怎么做才能使 hasRead<Y> 和 hasRead<Z> 失败,因为方法参数不对应于 std::uint16_t ?
我加入了我的代码,看看可以做些什么来让它像我想要的那样工作!
提前致谢:)
#include <cstdint>
#include <iostream>
#include <utility>
template<typename Class>
struct hasRead {
private:
template<typename T>
static constexpr auto check(T *) -> typename std::is_same<
decltype(std::declval<T>().read(std::declval<uint16_t>())), uint8_t>::type;
template<typename>
static constexpr std::false_type check(...);
typedef decltype(check<Class>(0)) type;
public:
static constexpr bool value = type::value;
};
struct X {
uint8_t read(uint16_t x) { return 3; }
};
struct Y {
uint8_t read(uint8_t x) { return 3; }
};
struct Z {
uint8_t read(int64_t x) { return 3; }
};
static_assert(hasRead<X>, "");
static_assert(hasRead<Y>, "");
static_assert(hasRead<Z>, "");
【问题讨论】:
标签: c++ c++11 templates sfinae