-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
type_trait里is_constructible的实现:
934 struct __do_is_direct_constructible_impl
935 {
936 template<typename _Tp, typename _Arg, typename
937 = decltype(::new _Tp(declval<_Arg>()))>
938 static true_type __test(int);
939
940 template<typename, typename>
941 static false_type __test(...);
942 };
943
944 template<typename _Tp, typename _Arg>
945 struct __is_direct_constructible_impl
946 : public __do_is_direct_constructible_impl
947 {
948 typedef decltype(__test<_Tp, _Arg>(0)) type;
949 };
950
951 template<typename _Tp, typename _Arg>
952 struct __is_direct_constructible_new_safe
953 : public __and_<is_destructible<_Tp>,
954 __is_direct_constructible_impl<_Tp, _Arg>>::type
955 { };
前面__do_is_direct_constructible_impl里定义了两个static的同名函数__test,这两个只有一个会生效。哪个生效取决于936行typename = decltype(::new _Tp(declval<_Arg>())这个模板参数是不是合法:如果_Tp, _Arg分别是int, int,则decltype(::new int(declval<int()))是合法类型,则938行的__test生效,返回值是true_type;如果_Tp, _Arg分别是int, std::string,则941行的__test生效,返回值是false_type。936行的模板比940行的模板更特殊,按照SFINAE的规则,两者都成立时936行更优先。
相应地,948行的"type"在上述两种情况下分别是true_type, false_type。这个type也就是954行__is_direct_constructible_impl<_Tp, _Arg>的::type。这个type和953行is_destructible<_Tp>共同决定了__and_的结果。
最终std::is_constructible<int, int>是否成立会调到上述路径上(中间还经历了一些其它路径)。
type_trait里面还有很多其它类型检测用了934行那里的技巧,关键是937行那样的表达式检测。
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels