C ++ template parameter and error c3201

Source: Internet
Author: User

Recently I learned C ++ template and found that you can use template parameter.

Then I made a small part.Code..

// Template parameter <br/> template <typename T, template <typename x> class cone = STD :: deque> <br/> class myclass <br/>{< br/> Public: <br/> cone <t> A; <br/> myclass () <br/>{< br/>}< br/> };

The compiler reports an error: Error c3201: The template parameter list for class template 'std: deque 'does not match the template parameter list for Template parameter 'cone'

I checked this error and said: Do not add a template without a template ..

But here I am using the standard STL deque. There is no reason to say this.

Then I tried again.

// Template parameter <br/> template <typename T, class cone = STD :: deque <t> <br/> class myclass <br/>{< br/> Public: <br/> cone a; <br/> myclass () <br/>{< br/>}< br/>}; <br/>

Haha, this can be compiled. No problem. Can I use this code to replace the above Code? I found out. There will be security issues. One case is myclass <int, deque <int>. The other case is myclass <double, deque <int>. if we may put the first T type variable into cone, it is inevitable to have a numerical conversion problem here. Precision is lost and a bug may occur. However, if myclass <STD: String, deque <const char *> is used, compilation may occur. So how can we avoid this problem? Use the following code:

// Template parameter <br/> template <typename T, template <typename x> class cone> <br/> class myclass <br/>{< br/> public: <br/> cone <t> A; <br/> myclass () <br/>{< br/>}< br/>}; <br/>

In this way, the variable type T can be consistent with the type in cone. If I still want to have a default template, so that I don't have to add STD: deque every time. So I still don't return to the first piece of code? The error c3201 is still thrown. Later, I checked the deque constructor in the standard STL library. deque requires two parameters: deque <int, Allocator <int>, the reason for doing this is that every time you apply for a space, it is based on the size of the Int. Of course, STL allows you to define your own allocator. The following write operations can be both secure and flexible:

// Template parameter <br/> template <typename T, typename A, template <typename X, class alloc> class cone = STD :: deque> <br/> class myclass <br/>{< br/> Public: <br/> cone <t, a> A; <br/> myclass () <br/>{< br/>}< br/>}; <br/>

when using the template, pay attention to the following points: one is the template class cone> in the template parameter. Remember that this must be a class. generally, we use template, and the class and typename have no difference. But here is an exception. Typename is the type name (either the class name or the basic aribtrary type), and the class may be a template, class, or struct. For example, STD: deque is the Template Name, while STD: deque is the class name. So there is a difference between the two. In addition, some compilations can successfully compile my first piece of code. I use vs2005. In the second template, typename X and class alloc are just a document parameter, which has no practical effect. Only to mark cone. It can be considered similar to function declaration.

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.