Home > Back-end >  C how to "un-use" a library reference?
C how to "un-use" a library reference?

Time:12-30

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related