คำถามติดแท็ก 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); }; …



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 หรือไม่? …
13 c++  typetraits 

1
เหตุใดจึงต้องมี static_cast ในการใช้งาน gcc ของ is_nothrow_constructible
นำมาจากการใช้ 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
เหตุใดจึงไม่จำเป็นต้องใช้ชื่อพิมพ์สำหรับประเภทที่อ้างอิงในกรณีต่อไปนี้
ผมได้อ่านเกี่ยวกับการลบการอ้างอิงจากประเภทที่นี่ มันให้ตัวอย่างต่อไปนี้: #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 …
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.