Questions tagged «typetraits»

5
is_base_of是如何工作的?
以下代码如何工作? typedef char (&yes)[1]; typedef char (&no)[2]; template <typename B, typename D> struct Host { operator B*() const; operator D*(); }; template <typename B, typename D> struct is_base_of { template <typename T> static yes check(D*, T); static no check(B*, int); static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes); }; …


3
平凡,标准布局,POD
用外行的话来说,平凡类型,标准布局类型和POD有什么区别? 具体来说,我想确定是否new T与new T()任何模板参数不同T。哪种类型的性状is_trivial,is_standard_layout而且is_pod我应该选择? (作为附带的问题,这些类型特征中的任何一种都可以在没有编译器魔术的情况下实现吗?)


1
std :: is_constructible为私有构造函数返回不一致的值
std::is_constructible处理私有构造函数的规则是什么?给出以下代码: #include <iostream> class Class { private: Class() { } }; template <typename T> class Test { public: static void test() { std::cout //<< std::is_constructible<Class>::value << std::is_constructible<T>::value << std::endl; } }; int main() { Test<Class>::test(); } 此打印0(ideone),即T默认情况下不可构造。 取消注释行的注释,它打印11(ideone),因此T突然变为默认可构造的。 我可以找到支持这两个结果的理由,但是我不明白包括注释行如何改变第二个结果。这是以某种方式调用UB的吗?这是编译器错误吗?还是std::is_constructible真的不一致?
13 c++  typetraits 

1
为什么在gcc的is_nothrow_constructible实现中需要static_cast?
取自GCC实施type_traits为什么static_cast在这里需要? template <typename _Tp, typename... _Args> struct __is_nt_constructible_impl : public integral_constant<bool, noexcept(_Tp(declval<_Args>()...))> {}; template <typename _Tp, typename _Arg> struct __is_nt_constructible_impl<_Tp, _Arg> : public integral_constant<bool, // Why is `static_cast` needed here? noexcept(static_cast<_Tp>(declval<_Arg>()))> {};

3
在以下情况下,为什么不需要对依赖类型使用typename?
我一直在这里阅读有关删除类型引用的信息。 它给出以下示例: #include <iostream> // std::cout #include <type_traits> // std::is_same template<class T1, class T2> void print_is_same() { std::cout << std::is_same<T1, T2>() << '\n'; } int main() { std::cout << std::boolalpha; print_is_same<int, int>(); print_is_same<int, int &>(); print_is_same<int, int &&>(); print_is_same<int, std::remove_reference<int>::type>(); // Why not typename std::remove_reference<int>::type ? print_is_same<int, std::remove_reference<int &>::type>();// Why …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.