我正在努力了解它的实现std::is_class.我复制了一些可能的实现并编译它们,希望弄清楚它们是如何工作的.完成后,我发现所有的计算都是在编译期间完成的(因为我应该早点想出来,回头看看),所以gdb可以不再详细介绍究竟发生了什么.
我正在努力理解的实现是这样的:
template<class T, T v>
struct integral_constant{
static constexpr T value = v;
typedef T value_type;
typedef integral_constant type;
constexpr operator value_type() const noexcept {
return value;
}
};
namespace detail {
template <class T> char test(int T::*); //this line
struct two{
char c[2];
};
template <class T> two test(...); //this line
}
//Not concerned about the is_union<T> implementation right now
template <class T>
struct is_class : std::integral_constant<bool, sizeof(detail::test<T>(0))==1
&& !std::is_union<T>::value> {};
Run Code Online (Sandbox Code Playgroud)
我在使用两条注释行时遇到了麻烦.第一行:
template<class T> char test(int …Run Code Online (Sandbox Code Playgroud)