0

When invoking a standard method, for example, std::sort(), we just need a namespace and can further simply by using namespace std; How can I do this with a user-defined class or is it impossible. (No pre-processor macro solution please). I tried using this:

#include <iostream>
using namespace std;
namespace std
{
    class A
    {
    public:
        static void foo()
        {
            std::cout << "foo";
        }
    };
}
int main ()
{
    //foo(); does not work
    A::foo(); //Only this works
}

Replacing std by any other namespaces also doesn't work.

Scripter
  • 1
  • 1
  • 2
    you are not allowed to add an `A` to the `std` namespace. Irrespective of how you call the method, the code has UB – 463035818_is_not_an_ai Sep 23 '21 at 09:07
  • 1
    why is `foo` not a free function? If you want it to be a free function, just make it one – 463035818_is_not_an_ai Sep 23 '21 at 09:07
  • Putting everything ugly together, it can be done: [demo on coliru](http://coliru.stacked-crooked.com/a/046f109b791a9c91). But I carefully would remove any traces which could lead back to me being the author... ;-) – Scheff's Cat Sep 23 '21 at 09:12
  • Can you give an example what is not working? – RoQuOTriX Sep 23 '21 at 09:13
  • @463035818_is_not_a_number I tried using other namespaces but none of them work – Scripter Sep 23 '21 at 09:23
  • Just in case you didn't know, `cin` or `std::cin` is not a method, it's an object (an `std::istream`). – WBuck Sep 23 '21 at 09:27
  • what are you actually trying to achieve? Frankly, this looks like lazyness of typing which is common among beginners. Scopes are a good thing and there are good reasons to write `A::foo` instead of `foo` when `foo` is from `A`. From a design point of view `foo` should not be a (static) member of `A` in the first place, because it doesn't need to be – 463035818_is_not_an_ai Sep 23 '21 at 09:29
  • Change your `namespace` to `test`. Then remove your current `using namespace std;` and in `main` write `using namespace test`. So `int main( ) { using namespace test; A::foo( ); }` – WBuck Sep 23 '21 at 09:29
  • @463035818_is_not_a_number Yes, I know, but I was just wondering if it works or not – Scripter Sep 23 '21 at 09:33

1 Answers1

2

When invoking a standard method, for example, std::sort(), we just need a namespace and can further simply by using namespace std; How can I do this with a user-defined class [...]

std::sort is a template not a function, but otherwise it is not different from code you can write yourself:

 namespace foo {
       void bar() {}
 }

 using namespace foo;

 int main() {
     bar();   // ok
 }

This works for namespaces but not for members of classes (there is using for members of classes to bring something from a base class scope into the derived class scope, but thats a different topic and beyond the scope of the question (no pun intended), it is not what you are asking for here).

In your example there is no reason why foo should be a member of A, hence you should make it a free function (as above). Supposed you didn't write A but you still need to call it without qualifiying the class name you can wrap it in a free function.

Also note that you are not allowed to add something to namespace std. If you do, your code has undefined behavior.

And finally, note that there are good reasons to discourge usage of using some_namespace; altogether. Consider this:

   namespace foo1 {
         void bar(){}
   }
   namespace foo2 {
         void bar(){}
   }

   // lots of other code

   using namespace foo1;

   // lots of other code

   int main() {
         foo1::bar();    // this is bar from foo1
         foo2::bar();    // this is bar from foo2
         bar();          // to know which bar this is you have to 
                         // look for any using directive, ie 
                         // it makes your code much harder to read
   }

Code is written once but read many times. The 6 extra characters to type pay off in more clear code. See also: Why is “using namespace std;” considered bad practice?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185