0

To ensure readability I want to keep the type names as short as possible. Below are 4 versions of a trivial code. What is the best practice to adopt? Are there even better ones?

Version a:

#include <iostream>

main ()
{
    ::std::string s;
    s = "hello";
    ::std::cout << s << ::std::endl;
}

Version b:

#include <iostream>

main ()
{
    std::string s;
    s = "hello";
    std::cout << s << std::endl;
}

Version c:

#include <iostream>

using namespace std;
main ()
{
    string s;
    s = "hello";
    cout << s << endl;
}

Version d:

#include <iostream>

main ()
{
    using namespace std;
    string s;
    s = "hello";
    cout << s << endl;
}
Pax to You
  • 71
  • 7
  • 6
    This is not opinion based. Always use `std::string`. Anything else makes your codebase vulnerable to namespace clashes, or is idiosyncratic. – Bathsheba Jan 13 '22 at 08:39
  • https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using "The use of using namespace std; leaves the programmer open to a name clash with a name from the standard library [...] However, this is not particularly likely to lead to a resolution that is not an error and people who use using namespace std are supposed to know about std and about this risk." – YSC Jan 13 '22 at 08:44
  • I strongly agree with @Bathsheba, it may not depend upon who you've met, which uses commands without namespace initialization, but people have different choices. – IneoSenigupta Jan 13 '22 at 08:45
  • title is about `std::string` vs `string` but the question is about `::std::string` vs `std::string`. – 463035818_is_not_an_ai Jan 13 '22 at 08:45
  • 1
    What is your question? What you ask in your text is different from your header. I think your real question is should we write "std::string" or "::std::string"? Use :: only for things in the global namespace (e.g. windows API calls), use the std::string notation anywhere else. (In any case don't use : "using namespace") – Pepijn Kramer Jan 13 '22 at 08:45
  • It ***is*** opinion-based, in my opinion. How about a line like `using std::string;` **inside** (first line of) a function that makes heavy use of `std::string`? – Adrian Mole Jan 13 '22 at 08:46
  • `std::string` vs `::std::string` can only cause confusion if you made excessive use of `using namespace ...` and there is a second `std::string` elsewhere. The latter is unlikely and the former is to be avoided anyhow – 463035818_is_not_an_ai Jan 13 '22 at 08:49
  • I've worked for a company, where they worked with the following namespaces: `.products..`, and at every occurence, the whole shebang was written, causing the code to be almost unreadable. For me, the biggest joke was that there were only namespaces for products, nothing else, and there was just one product, so `.` would have been enough :-) – Dominique Jan 13 '22 at 08:57
  • Related: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Jan 13 '22 at 09:19

1 Answers1

2

Is it a good idea in a .cpp file to always explicit the full name of a type (i.e. to write "std::string" instead of "string")?

Yes, it is a good idea to write std::string.

"String" is an extremely generic name, and a common programming concept. There are many string types across many libraries. You cannot know whether string refers to std::string or perhaps some other type in the current namespace, without parsing the entire code to see if there are using namespace or using declarations.

std is very specific. It's not a word in English. Programmers typically don't define namespaces by that name (although it would technically be allowed to define another_ns::std). Programmers immediately know that std::string refers to, with a very high likelihood, ::std::string.

Knowing what entities names refer to is an essential part of understanding source code.

eerorika
  • 232,697
  • 12
  • 197
  • 326