Introduction to template metaprogramming 2

Source: Internet
Author: User
2: Calculate the multiplication of a number. Of course, this can be done by using the pow function in the cmath header file. However, this method is less efficient for the multiplication of long integers with a small number of times. It is better to manually write an operand concatenation expression, but sometimes this is not convenient, especially when the base number of the multiplication operator itself is a more complex expression

2: Calculate the multiplication of a number. Of course, this can be done by using the pow function in the cmath header file. However, this method is less efficient for the multiplication of long integers with a small number of times. It is better to manually write an operand concatenation expression, but sometimes this is not convenient, especially when the base number of the multiplication operator itself is a more complex expression

2: Calculate the multiplication of a number.

Of course, this can be done by using the pow function in the cmath header file. However, this method is less efficient for the multiplication of long integers with a small number of times. It is better to manually write an operand concatenation expression, but sometimes this is not convenient. Especially when the base number of the multiplication operator is itself a complicated expression, it is usually necessary to save the expression with a temporary variable and then multiply the temporary variable. Defining the following inline function provides some convenience.


Inline double power (double x, unsigned n)

{

Double result = x;

For (int I = 1; I <n; I ++)

Result * = x;

Return result;

}

When n is relatively small, the efficiency of this function is usually higher than that of the pow function in the cmath header file. However, this function must execute a loop during runtime and does not achieve the ideal efficiency, the template element came in handy again. As follows:

Template

Inline double power (double v)

{

Return v * power (V );

}

Template <>

Inline double power <1> (double v)

{

Return v;

}


The template above is not general enough and can only be of the double type. The new type parameter T is referenced below. Because the function template does not support the special feature, we cannot directly specify the result when N = 1, so we can use a class template.

Template

Struct Power

{

Template

Static T value (T x)

{

Return x * Power : Value (x );

};

Template <>

Struct Power <1>

{

Template

Static T value

{

Reurn x;

}


In this way, we can calculate the 4 Power of x and write: Power <4 >:: value (x );

However, this is inconvenient to write, so we can write an auxiliary template function, as shown below:

Template

{

Inline T Power (T v)

Return Power : Value (v );

}

In this way, the power of x can be written as follows: power <4> (x );

}

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.