Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - meaning of a statement combining typedef and typename [duplicate]

In a C++ header file, I am seeing this code:

typedef typename _Mybase::value_type value_type; 

Now, as I understand, quoting from « C++ the Complete Reference » by Schildt. typename can be substituted by keyword class, the second use of typename is to inform the compiler that a name used in a template declaration is a type name rather than an object name.

Similarly, you can define new data type names by using the keyword typedef. You are not actually creating a new data type, but rather defining a new name for an existing type.

However, can you explain exactly what is the meaning of the above line of code, where typedef and typename are combined together. And what does the "::" in the statement imply?

like image 784
Arvind Avatar asked Aug 22 '13 15:08

Arvind


People also ask

What is typedef Typename?

typedef is defining a new type for use in your code, like a shorthand. typedef typename _MyBase::value_type value_type; value_type v; //use v. typename here is letting the compiler know that value_type is a type and not a static member of _MyBase . the :: is the scope of the type.

What is Typename in C?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.

Does typedef create a new type?

typedef simply lets you give a new name to an existing type. But that name combines every aspect of the existing type into an indivisible entity, such that e.g. ptype a, b; is equivalent to ptype a; ptype b; (and const ptype means "const pointer-to-int" because ptype means "pointer-to-int").

Should typedef be public or private?

Any typedef used in the public interface of the class should be in the public section of the class. Rest should be private . Show activity on this post. If you declare as private , you will not be able to use them outside your class.


2 Answers

typedef is defining a new type for use in your code, like a shorthand.

typedef typename _MyBase::value_type value_type; value_type v; //use v 

typename here is letting the compiler know that value_type is a type and not a static member of _MyBase.

the :: is the scope of the type. It is kind of like "is in" so value_type "is in" _MyBase. or can also be thought of as contains.

like image 82
pippin1289 Avatar answered Oct 14 '22 00:10

pippin1289


the typename is saying that _Mybase::value_type is the name of type so the typedef can reley on that fact.

like image 23
Paul Evans Avatar answered Oct 14 '22 00:10

Paul Evans