0

In a program I am making heavy use of the std library. I want to name a function less(), however this is already taken in std. Is there a line I can add after using namespace std; that will clear out less() for declaration later.

Currently I am receiving "error: reference to 'less' is ambiguous".

I am aware that I can list out everything that I am using (e.g. using std::cout;), I just wanted to ask if there is a 'negated' version of this.

Thank you, Daniel

  • 6
    this is a perfect example of why you should never use 'using namespace std', see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – pm100 Dec 29 '21 at 23:07
  • No. Once done, the effects of a `using` directive (like `using namespace std`) cannot be undone. The solutions are either to avoid the `using` directive, or fully qualify the names (e.g. call your function `::less` if it is not in any namespace, or `yournamespace::less` if it is in `yournamespace`). – Peter Dec 29 '21 at 23:32

3 Answers3

9

You can use explicitly ::less or use using ::less in inner scope.

#include <iostream>

using namespace std;
void less();

void func1() {
    ::less();
}

void func2() {
    using ::less;
    less();
}

Overall, instead, consider removing using namespace std; from your code and decorate your code with std:: everywhere where it is needed.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
4

This is easy to answer: no, that is impossible. You have to avoid polluting your namespaces in the first place.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Ralf Ulrich
  • 1,575
  • 9
  • 25
0

Is there a line I can add after using namespace std; that will clear out less() for declaration later.

No, but what you can do instead is to not write using namespace std; in the first place.

Or alternatively, you can refer to your less with a qualified name using the scope resolution operator.

eerorika
  • 232,697
  • 12
  • 197
  • 326