C ++ template metaprogramming 7: traits class for Type judgment, template traits

Source: Internet
Author: User
Tags traits

C ++ template metaprogramming 7: traits class for Type judgment, template traits
2. traits class used to determine the type

The is_void class can be used to determine the type of template parameters. It is easy to use. You can also use: value directly.

Cout <std: is_void <void>: type: value <endl; cout <std: is_void <bool>: type: value <endl;

However, the implementation is interesting. The special feature can be used to inherit different classes. The clang ++ implementation code is not found here. Use boost code instead.

Template <typename T> struct is_void: public false_type {}; template <> struct is_void <void>: public true_type {};

The c ++ 11 standard also supports is_pointer, is_floating_point, is_array, is_lvalue_reference, is_rvalue_reference, is_member_object_pointer, struct, is_enum, is_union, is_class, is_function. The implementation code is very simple. Here is another example:

Template <typename T> struct is_pointer: public false_type {}; template <typename T> struct is_pointer <T *>: public true_type {};

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.