0

this is my first post, I am a beginner. I'm learning.

I've read that using using namespace std is considered a bad practice and I kinda understood why, so I'm trying to use std:: every time. I know I have to use it in cout and cin.

My question is, in what other functions do I got to use std::? Where can I see a list or something like that?

Thanks.

  • 2
    Everything in the C++ Standard starts with `std`. [Pretty big list](https://en.cppreference.com/w/). The stuff that was adopted from C may be a little bit different. For example, `std::pow` is not exactly the same as `pow`. – user4581301 Feb 04 '21 at 02:12
  • http://eel.is/c++draft/ tracks to (as far as I know) the most recent version of the draft. You'll have to fudge around a bit to find the revisions that most closely match the released Standard you're compiling for. There's a list of the mapping somewhere on Stack Overflow, but I can't find it at the moment. – user4581301 Feb 04 '21 at 02:21
  • Found the sucker: [Where do I find the current C or C++ standard documents?](https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) – user4581301 Feb 04 '21 at 02:26
  • cppreference.com is a good place to start for exploring the STL, and get teh latest online documentation. Using the std from the start is good. Have fun! – Michaël Roy Feb 04 '21 at 13:03

2 Answers2

1

The list is really big. But the compiler will warn you if you don't use it when you need it. It helps if you use a decent IDE. There are a bunch that are free to use, so it's just a matter of picking one.

Pretty much everything you might call needs some sort of namespace prefix, with std:: being the most common.

Note that it's not so bad if you do something like this:

using std::cout;
using std::endl;

cout << "This is a string" << endl;

But if you do:

using namespace std;

Then you're basically pulling in all of std -- which increases the likelihood of hitting name collisions.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
0

Check/test this code on https://repl.it/@JomaCorpFX/NameCollision#main.cpp

#include <iostream>

namespace std{
  static int Add(int a, int b)
  {
    return a + b;
  }
}

namespace math{
  static int Add(int a, int b)
  {
    return a + b;
  }
}

static int Add(int a, int b)
{
  return a + b;
}

using namespace std;
using namespace math;

int main() {
  std::cout << std::Add(1,2) << std::endl; //OK
  std::cout << math::Add(1,2) << std::endl; //OK
  std::cout << ::Add(1,2) << std::endl; //OK
  std::cout << Add(1,2) << std::endl;//ERROR. Ambiguous call to "std::Add" or "math::Add" or local function "Add" // Comment this line for fix the error or complete the correct namespace or delete using namespace statements.
}

Output when compile

main.cpp:29:16: error: call to 'Add' is ambiguous
  std::cout << Add(1,2) << std::endl;//ERROR. Ambiguous call to "std::Ad...
               ^~~
main.cpp:4:14: note: candidate function
  static int Add(int a, int b)
             ^
main.cpp:11:14: note: candidate function
  static int Add(int a, int b)
             ^
main.cpp:17:12: note: candidate function
static int Add(int a, int b)
           ^
1 error generated.
compiler exit status 1

If you type "using namespace std" statement you can incurr in function name/signature collision, another headers can contains the same function signature without a namespace.

Your original code can breaks if you include a new header that can contains 1 or n functions which math the signature with your code. The compiler shows errors in code, many headaches.

Best practice is use the complete name, to avoid this. Spend more time, gain more readabilty and have less errors.

std::Add
math::Add
::Add

std::cout
std::string

For convention only C++ standard library can have the namespace std.

Joma
  • 3,520
  • 1
  • 29
  • 32
  • The standard prohibits adding your own functions to `std`. If you do, the behavior of your program is undefined. – Pete Becker Feb 04 '21 at 03:49