Follow C ++ 0x: Concept

Source: Internet
Author: User
The biggest change to generic editing in C ++ 0x is concept.
Concept is not just proposed. Concept is the foundation of STL.

STL is a role of C ++, and the most important one is the concept of iterator (concept. This is the most obvious concept obtained after exposure to STL. With the iterator, you can Algorithm And easy to separate, thus laying the foundation of STL. So one concept serving STL (concept) was born.
However, I believe most people only use STL directly, so they have no idea about concept. Once it involves self-Creation Program Libraries, especially when creating containers or algorithms compatible with STL specifications, must be exposed to a bunch of concept in STL.
For example, how to implement an iterator? There are several types of iterator in STL: forwarditerator, bidirectionaliterator, randomaccessiterator, and so on. Now we need to implement an iterator for the vector container, which is obviously consistent with the above three types. But the general class like forwarditerator must not be found from STL. A general definition of up to one iterator can be found: Template < Class Category, Class Type, Class Distance = Ptrdiff_t
Class Pointer = Type * , Class Reference = Type &>
Struct Iterator {
Typedef category iterator_category;
Typedef type value_type;
Typedef distance difference_type;
Typedef pointer;
Typedef reference;
};

It's very dizzy. It's all typedef. As for the more specific concept, we had to open the Standard C ++ documentation. We wanted to meet the requirements of forwarditerator and the requirements of randomaccessiterator, and then implemented them accordingly.
As to whether the implemented iterator meets the concept, you have to feel your own conscience and ask if it has been completely tested.
Why? Because these concept are currently only in the document. The C ++ compiler has no concept of concep. What's more, the C ++ compiler does not even see or only see a part of the iterator just defined-because most compilers compile only the classes or members that actually need to be implemented.

For example: Template < Typename t >
Class Test
{
Public :
Void Kill_cpp (T)
{
Don't call me!
}
};
Int Main ( Int Argc, Char * Argv [])
{
Test < Int > T;
// T. kill_cpp (0 );
Return   0 ;
}

The compilation is complete, and the C ++ CompilerDon't call me!. Sometimes I use this feature to ensure that some functions should not be called. Once called by mistake, I can also have a clear error message: Error c2065:'Don't call me!': Undeclared identifier (vc2008 beta2)

Maybe STL concept is less exposed. When writing generic programs, there will always be some similar situations. For example Code : Template < Typename t >
Static   Void Show ( Const T & T)
{
Cout < T. Message () < Endl;
}

This function is used to display information about a class. Obviously, this type must have the message member function. But do users of this function know? You may know, because your document is very detailed. However, once the user forgets or misuses it, the trouble will come out. It must be a bunch of error messages. This example is still relatively simple, so there are a few more error messages. You can check the information carefully to solve the problem. The following example:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> #include < List >
# include algorithm >
using namespace STD;

IntMain (IntArgc,Char*Argv [])
{
List<Int>LST;
Sort (lst. Begin (), lst. End ());
Return 0;
}

This example will generate a lot of error information, which is enough to make people who are new to them fall into the dark. The actual reason is that the iteraotr of list is not randomaccessiterator (that is, list cannot be randomly accessed as an array, so the sort function cannot be used ).

Java,. net, and other languages that are new to the generic programming features have a deep understanding of this. In other words, they all add the template constraint mechanism. If. NET is used to implement this sort, it may write as follows: VoidSort<T>(T container) where T: ienumerable
{

}

In this way, when the container is substituted into a total of its types, such as int, a clear error will be given during compilation: The Type ' Int ' Cannot be used As Type parameter ' T '   In The generic type or Method Sort <t> (t) ' .
There Is No boxing conversion from ' Int ' To ' System. Collections. ienumerable ' . (VC #2008 beta2)

Yes, template constraints are very useful. At least we can rest assured that the compiler will help us check what conditions T should meet. So I began to envy Java and C #: after all, it was a latecomer who adopted the advantages of C ++ and supplemented the disadvantages...
In order to make C ++ have this feature, the C ++ template library writers are really painstaking. Technologies such as template meta-programming were born. Boost has a considerable number of program classes in this effort. However, all the efforts can only improve the ease-of-use of the library, but greatly increase the complexity and maintainability of the library. The final solution is to build relevant features in the C ++ language.

Hope, finally, C ++ 0x. What's surprising is that C ++ does not adopt the Java and C # template constraints, instead, the concept of STL is changed from document to language. I think this change is revolutionary, and it has brought about an unprecedented new programming method that will create a new popularity since the template-foreseeable, C # And java will eventually adopt this feature.
Concept applications include the template constraints in C # and Java, but they are not limited to this. Template constraints are only basic.

For the above sort example, a concise error message will appear in C ++ 0x: Sort. cpp: In Function ' Void F () ' :
Sort. cpp: 7 : Error: no matching function For Call ' Sort (STD: _ list_iterator <int>, STD: _ list_iterator <int>) '
< Path > : Note: candidates are: Void STD: Sort (_ ITER, _ ITER) [with _ ITER = STD: _ list_iterator < Int > ] < Where clause >
Sort. cpp: 7 : Note: No concept map For Requirement ' STD: mutablerandomaccessiterator <STD: _ list_iterator <int> '

Error message: the available sort function cannot be found.
The following message is displayed: List iterator does not have a muting to mutablerandomaccessiterator.
Just a little bit, you will understand that sort is an iterator that requires random access, and list does not have any iterator of the class or can map to the iterator of the class.
There are two solutions: one is to replace the container with an iterator with random access, such as vector; the other is to implement a muting to mutablerandomaccessiterator.

Currently, the concept proposal in C ++ 0x has been confirmed to be passed. Concept introduces C ++ to make the template library easy to use and write.

If you are interested in concept, you can view related documents. Maybe because of the many exciting features of C ++ 0x, I will use my method to express them one by one.

Related links:
Http://del.icio.us/pongbablog/cplusplus
C ++ 0x series: concept and concept!
C ++ 0x outlook [language core Evolution]
Oh, it's a bit lazy, Liu weipeng.ArticleWe recommend users who are interested in C ++ 0x to watch the videos. The documents attached to this article and related standard proposals are quite detailed. I will not copy it here.

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.