C++11:static_assert and type traits

Source: Internet
Author: User

Static_assert provides a compile-time assertion check. If the assertion is true, nothing will happen. If the assertion is false, the compiler prints a special error message.

12345678910111213 template <typename T, size_t Size>class Vector{   static_assert(Size < 3, "Size is too small");   T _points[Size];};int main(){   Vector<int, 16> a1;   Vector<double, 2> a2;   return 0;}
1234567 error c2338:size is too small see reference to class template instantiation ' vector<t,size> ' being compiled Code class= "Text Spaces" >&NBSP;&NBSP;&NBSP; with     [         t=double,         size=2 &NBSP;&NBSP;&NBSP; Code class= "Text plain" >

Static_assert and type traits can be used together to achieve greater power. Type traits is a class that provides information about the type at compile time. You can find them in the header file <type_traits>. There are several types of class:helper classes in this header file that are used to generate compile-time constants. Type traits class, which is used to obtain type information at compile time, as well as the type transformation class, where they can transform existing types into new types.

The following code originally expected to be used only for integer types.

12345 template<typename T1, typename T2>auto add(T1 t1, T2 t2) -> decltype(t1 + t2){returnt1 + t2;}

But if someone writes the following code, the compiler does not give an error.

12 std::cout << add(1, 3.14) << std::endl;std::cout << add("one", 2) << std::endl;

The program will print out 4.14 and "E". But if we add a compile-time assertion, the above two lines will produce a compilation error.

12345678 template < typename t1, typename T2> auto Add (T1 t1, T2 T2) decltype (t1 + T2) { &NBSP;&NBSP;&NBSP; static_assert (std::is_integral<t1>::value, &NBSP;&NBSP;&NBSP; static_assert (std::is_integral<t2>::value,  &NBSP;&NBSP;&NBSP; return t1 + T2; }
1234567891011121314 error C2338: Type T2 must be integralsee reference to function template instantiation ‘T2 add<int,double>(T1,T2)‘ being compiled   with   [      T2=double,      T1=int   ]error C2338: Type T1 must be integralsee reference to function template instantiation ‘T1 add<const char*,int>(T1,T2)‘ being compiled   with   [      T1=const char *,      T2=int   ]

C++11:static_assert and type traits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.