我想知道一个类里有没有定义 value_type,但是为什么 has_value_type_1 可以做到,has_value_type_2 做不到。查了半天了,还是没搞明白。
// c++98 version
#include <iostream>
struct false_type { const static bool value=false; };
struct true_type { const static bool value=true; };
template <typename> struct type_sink { typedef void type; };
template <typename, typename = void> struct has_value_type_1 : false_type {};
template <typename, typename = void> struct has_value_type_2 : false_type {};
template <typename T> struct has_value_type_1< T, typename type_sink< typename T::value_type >::type > : true_type {};
template <typename T> struct has_value_type_2< T, typename T::value_type > : true_type {};
struct A { typedef int value_type; };
struct B { };
template <typename T>
bool f_1(T) { return has_value_type_1<T>::value; }
template <typename T>
bool f_2(T) { return has_value_type_2<T>::value; }
#include <iostream>
int main()
{
A a;
B b;
bool x[4] = {f_1(a), f_1(b), f_2(a), f_2(b)};
for(int i=0;i<4;i++)
std::cout<<x[i]<<' ';
return 0;
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.