Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Should I use 'typedef' or 'using namespace'? [closed]

I am writing a library with mutiple dependent modules. When I include a file from a different module, should I resolve the namespace with:

using namespace project1::namespace1; class1 obj; 

or

typedef project1::namespace1::class1 class1; class1 obj; 

What are the pros/cons of each approach? I read somewhere that we should use typedef in .H files and using in .C files, is this advisable?

One problem I have encountered with 'typedef' is it leads to namespace ambiguity if I include both original class and the class with 'typedef' in a third module.

like image 284
vid Avatar asked Apr 26 '12 10:04

vid


People also ask

Should typedef be in namespace?

A typedef can go either outside a namespace (which makes it global and causes it to apply everywhere), or inside (which causes it to be scoped to that namespace).

Is using better than typedef?

In C++, 'using' and 'typedef' performs the same task of declaring the type alias. There is no major difference between the two. 'Using' in C++ is considered to define the type synonyms.

Is it good practice to use namespace?

The statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.

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.


2 Answers

The two options you state are not equivalent. This one:

using namespace project1::namespace1; 

pulls in everything from the namespace, giving you little control and making clashes likely. I see only cons, and no pros here.

But you don't need to use a typedef to bring in a single symbol, you can use

using project1::namespace1::class1; 

Whether you use this or the typedef doesn't make too much of a difference. But bear in mind that typedef is limited to types and enumerations, whereas using can refer to values, functions, etc:

namespace X {   const int x{42};   enum Fruit{Apple, Pear}; }  using X::x; // OK typedef X::x xx; // Error! 'x' in namespace 'X' does not name a type 

so the two expressions are not completely equivalent.

like image 67
juanchopanza Avatar answered Oct 02 '22 07:10

juanchopanza


You should never use using or typedef in a header file just for the sake of making names easier to type.

In the source file, it's up to you. It seems to be considered good practice to write out the whole name, as it makes it very clear what you meant. If your namespace is too long, you can use a namespace alias to reduce clutter, but still keep the meaning clear: namespace ns = project1::namespace1;

Either way, if you're going to import symbols into the global namespace, use using, not typedef. typedef is used mainly when you want to call the type by a different name, many times because it is a template - for example, my_map instead of std::map<std::string, my_type>, which is still clear, but much nicer to type.

Also, see this question: Why is "using namespace std" considered bad practice?

like image 35
parkovski Avatar answered Oct 02 '22 08:10

parkovski