Function of the C ++ typename keyword

Source: Internet
Author: User

Reference: callback

 

Description:

2. type description typename

The typename keyword tells the compiler to interpret a special name as a type. The typename keyword must be used for a name in the following cases:

1.A unique name (which can be understood as a type) is nested in another type.

2.Depending on a template parameter, that is, the template parameter contains this name to some extent. A misunderstanding occurs when the template parameter causes the compiler to identify a type.

To be safe, you should use typename where all compilers may mistakenly regard a type as a variable. In the following example

Typedef Cone: one_value_typeTwo_value_type; when compiling the ctwo class, the compiler may regard 'cone: one_value_type 'as a variable, because typename is used in'/* 2, the compiler knows that it is a type and can be used to declare and create instances.

Give You A Concise Guide: If your type is limited in template parameters, you must use typename.
# Include "iostream"

# Include "typeinfo"// For typeid () Operator

Using namespace STD;
Template
Struct cone
{ // Default member ispublic
   Typedef tpone_value_type;
};
Template // Use a template class as the template parameter, which is very common
Struct ctwo
{
   // Note the following two lines
   // Typedefcone: one_value_typeTwo_value_type;// * 1

   Typedeftypename Cone: one_value_typeTwo_value_type; // * 2

};
// The above two template classes only define two internal public types, but please note that the two_value_type of the second ctwo class
// The one_value_type dependent on cone, and the latter depends on the parameter type passed in when the cone template class is instantiated.
Int main ()
{
   Typedef coneoneint_type;
   Typedefctwo <oneint_type> twoint_type;
   Twoint_type: two_value_type I;
   Int J;
   If (typeid (I) = typeid (j )) // If I is an int variable
       Cout <"Right! "<Endl; // Print right
   Return;
}

In Linux, the above example is compiled with G ++ 2.93 and the result is printed with "right". However, if the Comment Number of line * 1 is removed, comment
* If there are two rows, an error is reported during compilation. The Compiler does not know what cone: one_value_type is. Generally, the type in the template class parameter is
The actual body is revealed only after instantiation, but the ctwo class depends on an existing cone template class.
Make sure that the ctwo: two_value_type and cone: one_value belong to the same type. If this is the case, please ask typename to go out and tell
The cone: one_value_type after the compiler is a type name that already exists in a certain place.
It can work smoothly.

 

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.