Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ good coding style - always fully qualify library types?

What is generally considered good coding style in C++ where you use types from the standard library? For example, if I have a using namespace std; directive would you still expect to see library types fully qualified like so: std::string or is it acceptable to just use string as the type identifier?

If you do fully qualify, can you expain the rationale behind it?

like image 580
flesh Avatar asked Dec 09 '09 14:12

flesh


2 Answers

fully qualify in header files. import the namespace in the .cpp files.

keeps the global namespace from being cluttered by a simple #include

like image 165
sdellysse Avatar answered Oct 14 '22 10:10

sdellysse


I'd prefer using:

using std::string;
string whatever;

than fully bringing the namespace in.

In any case library developers should avoid type names that conflict with the standard ones although string probably is quite common.

For libraries other than standard I like to qualify if the nesting namespaces is not too long, if it is just typedef it to a meaningful name that includes the library name or something like that.

like image 31
Arkaitz Jimenez Avatar answered Oct 14 '22 08:10

Arkaitz Jimenez