C ++: metaprogramming

Source: Internet
Author: User

Today, I flipped through "C ++ template" in the bookstore and learned metaprogramming, which I had never understood before. I found that it was the application that used to add Enum to the template, taking a closer look, we can derive a lot of usage. The following is the simplest example:
# Include <iostream>
Using namespace STD;

Template <int n>
Class twopower
{
Public:
Enum {result = 2 * twopower <n-1 >:: result };

};

Template <>
Class twopower <0>
{
Public:
Enum {result = 1 };
};

Int main ()
{
Cout <twopower <5 >:: result <Endl;
}

Defines a template used to calculate the n power of 2, and uses the template features as a recursive termination condition. The first time I came into contact with this form of template application was in typelist of "modern c ++ design", I was very surprised to find that the expression ability of C ++ is really unimaginable.

Today, I think that this technology can be used to implement something similar to the attribute in. Net during compilation. Come back and try it on the machine, like this:
First, define a template class of attribute and provide a default value:

Template <typename T>
Class serializableattribute
{
Public:
Enum {isserializable = 0 };
};

Then you define your own class, such:

Class myclass
{
};

To apply the attribute above, you need to provide a special feature for your class:

Template <>
Class serializableattribute <myclass>
{
Public:
Enum {isserializable = 1 };
};

This attribute may be used elsewhere. (Reflect? )

Int main ()
{
If (serializableattribute <myclass >:: isserializable) cout <"myclass can be serialized." <Endl;
Else cout <"myclass can't be serialized" <Endl;
}

I think the above applications should be the reason why this technology is called metaprogramming. Such attribute and. the biggest region of attribute in net is that it is static, because the derivation of the template is all completed during the compilation period, not like. net, and then use reflection to get the metadata in the execution period. Of course, this will limit its usage.

However, sometimes I wonder whether the lack of runtime support like C ++ is a disadvantage? Or advantages? Recently, I have been studying the generic mechanism in C #2.0. The newly proposed constraint mechanism is probably due to C #'s powerful reflection mechanism, which leads to generics derivation, it cannot be added only when the compilation period is complete like C ++.

the compilation phase polymorphism in C ++ does not seem to be available in C. However, it may be possible to add a Generic C # To produce many new applications. Who knows! When I first added a template for C ++, Professor stroustup did not think that generics would develop today. Language will always surprise us. Who said no.

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.