Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a typedef to self have any effect?

I've come across some C++ code that has the following:

typedef Request Request;

Is this just a no-op or does this typedef actual have an effect, and if so, what effect does it have?

like image 914
WilliamKF Avatar asked May 24 '12 15:05

WilliamKF


People also ask

Is it good to use typedef?

It can almost be necessary when dealing with templates that require multiple and/or variable parameters. The typedef helps keep the naming straight. Not so in the C programming language. The use of typedef most often serves no purpose but to obfuscate the data structure usage.

What is the point of typedef?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

What is typedef explain with an example?

typedef is used to define new data type names to make a program more readable to the programmer. For example: | main() | main() { | { int money; | typedef int Pounds; money = 2; | Pounds money = 2 } | } These examples are EXACTLY the same to the compiler.

Is typedef needed in C++?

typedef is necessary for many template metaprogramming tasks -- whenever a class is treated as a "compile-time type function", a typedef is used as a "compile-time type value" to obtain the resulting type.

Can you typedef a typedef?

Typedef cannot be used to change the meaning of an existing type name (including a typedef-name). Once declared, a typedef-name may only be redeclared to refer to the same type again.

Can typedef and struct have same name?

You can't "typedef a struct", that doesn't mean anything.


2 Answers

You can read all rules relative to typedef specifier for C++2003 ANSI ISO IEC 14882 2003 in section 7.1.3. In 7.1.3, 2) it have been said the identity typedef is allowed if the name already refers to some type.

This is legal:

typedef int Request;
typedef Request Request; // Redefines "Request" with no effect 

And that it is not:

typedef Request Request; // Illegal, first "Request" doesn't name a type. 

The standard has a specific example relating to this. C++2003, §7.1.3/2:

In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers. [Example:

typedef struct s { /* ... */ } s;
typedef int I;
typedef int I;
typedef I I;

end example]

In 7.1.3, 3) it have been said that use typedef to redefine type to alias to another type is prohibited

like image 177
Robᵩ Avatar answered Oct 11 '22 17:10

Robᵩ


If Request is only passed as a parameter it seems to be a opaque pointer.
There should be a

typedef struct Request Request 

somewhere in the code. (see comments on your question)
This is used to define an API and hide implementation details. So you can later change the implementation without changing the API again.

The client does not need to know anything about the acutal type - its just kind of a handle.
Everything you want to do with it has to be done with the api methods (creation, delete, load, init, ...)
Usually the handle Request will be casted to something more meaningfull in the implementation of the api. This was/is usually done in old C.

like image 22
G. Martinek Avatar answered Oct 11 '22 16:10

G. Martinek