C + + template meta-Programming VI: Integral_constant class

Source: Internet
Author: User
Tags traits

1Integral_constant class

This class is the base class for all traits classes, and provides the following features, respectively:

    • Value_type represents the type of value
    • Value means
    • Type represents itself, so you can use:: Type::value to get the value
    • True_type and False_type Two special classes are used to represent the traits of a bool value type, and many traits classes need to inherit them

The following code comes from C++11 and boost, respectively, slightly different:

    • C++11 contains the Value_type () function, which returns the true value
    • C++11 using the CONSTEXPR keyword to indicate execution at compile time
From C++11 standardnamespace std {  template <class T, t v>  struct Integral_constant {    static constexp R T value = V;    typedef T VALUE_TYPE;    typedef integral_constant<t,v> type;    constexpr operator Value_type () {return value;}  };  typedef integral_constant<bool, true> True_type;  typedef integral_constant<bool, false> False_type;} From Boosttemplate <class T, t val>struct integral_constant{   typedef integral_constant<t, val>  type;   typedef T                          Value_type;   static const T value = val;}; typedef integral_constant<bool, true>  true_type;typedef integral_constant<bool, false> false_type;

Here is the calling code to see the basic use method:

#include <iostream> #include <type_traits>using std::cout;using std::endl;int main () {  typedef std:: Integral_constant<int, 1> one_t;  cout << "One_t::value:" << one_t::value << Endl;  cout << "One_t::type::value:" << one_t::type::value << Endl;}

The output is:

One_t::value:1one_t::type::value:1

C + + template meta-Programming VI: Integral_constant class

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.