Differences between class and typename

Source: Internet
Author: User

I. Similarities:

There is no difference between class and typename in template definition.

Ii. Differences:

1. typename can indicate a nested dependency type name (Nested dependent type name), But inBase class listAndMember initialization listYou do not need to use "typename ";

Code:

 template 
   
     // you can use typename to replace Class A: public base :: B // base class list {public: typedef typename base: B info_type; A (): Base 
    
     :: B // member initialization list {}}; 
    
   

Nested dependency type name:

For this code, we can find that the actual implementation of base cannot be known during compilation, so base: B cannot be determined as a type of static member, if it is a member function of a class, there will be ambiguity here. In this case, the compiler defaults to"If the parser encounters a nested dependent name (nested dependency name) in a template, it assumes that the name is not a type ), unless you tell it in other ways ",In this case, we need typename to declare base: B as a type, but it can be omitted in the base class list and member initialization list.

2,Template <typename T, template <typename T> class S>OfS is actually a template parameterC ++ specifies that the template parameter can only be a class template. Therefore, changing the class to typename is not acceptable.


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.