C why typename




















The same thing happens when you want to refer to a template member of dependent type, i. You also have to help the compiler by using the keyword template , although it is placed differently. Of course, another role of the keyword typename is to be used in template parameter declarations.

The secret lies in the fact that a template can be specialized for some types. This means it also can define the interface completely different for several types. For example you can write:. One might ask why is this useful and indeed: That really looks useless.

Admittedly it doesn't change the kind of reference from a type to something different but nevertheless it could happen.

Now what happens if you write your own templates using this test template. Something like this. To tell the compiler what you expect you have to add a typename before. The correct template looks like this. Bottom line: You have to add typename before whenever you use a nested type of a template in your templates.

Of course only if a template parameter of your template is used for that inner template. I think all of the answers have mentioned that the typename keyword, is used in two different cases:. The exception is that if you use the nested dependant type name either as a base class or in a member initialization list , you should not use typename there:. Note: Using typename for the second case i. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Officially, what is typename for? Ask Question. Asked 12 years ago. Active 1 year, 5 months ago. Viewed 75k times. What's the story on typename? Improve this question. NAND 7 7 silver badges 22 22 bronze badges.

Compound types Union types. Default initialization Value initialization Zero initialization Copy initialization Direct initialization. Expressions Value categories Order of evaluation. Operators Operator precedence. Class declaration Constructors this pointer. Access specifiers friend specifier.

Class template Function template. Inline assembly. Keywords alignas. Compiler support. Freestanding and hosted. Language support library. Technical specifications. Flow control. Function declaration. Lambda function declaration. Fundamental types. Templates Keywords. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.

Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode.



0コメント