locked
namespace std RRS feed

  • Question

  • How can I add class or declaration in namespace std?
    Wednesday, May 19, 2010 5:27 AM

Answers

  • I think yiou should be able to extend std namespace. I have just tried the below code

    namespace std{
        class MyStd{
        public:
            MyStd(){}
            void foo()
            {   
                OutputDebugString(L"In MyStd Foo\n");
            }
        };
    }

    This I can use like std::MyStd std; in my main.

    • Marked as answer by Virgo2008 Wednesday, May 19, 2010 3:01 PM
    Wednesday, May 19, 2010 6:03 AM
  • As I said above, no. The std namespace isn't something which is magically in all files. In fact a standard .cpp file doesn't even have the std namespace until you include one C++ header file.

    What you want to do with that sample above REQUIRES that you put it in a header file snd then include it in all source files which make use of it.

    //source1.cpp
    
    namespace std
    {
      #define MyString string
    }
    
    using namespace std;
    
    class myclass
    {
      MyString str;
    };
    
    //source2.cpp
    
    using namespace std;
    
    class myclass2
    {
      MyString str; //this WILL fail because MyString hasn't been defined.
    };

    This will result in 3 error messages because MyString is undefined in source2.cpp.

    The errors are

    error C2146: syntax error : missing ';' before identifier 'str'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    If however you put it into a header and include that everything will work out fine.

    //myheader
    //remember to prevent multiple inclusion problems by either using #pragma once or
    //by defining a preprocessor definition
    #pragma once
    
    namespace std
    {
      #define MyString string //this is ok, just remember to include <string> before you attempt to use it
    }
    
    //source1.cpp
    #include "myheader"
    
    using namespace std;
    
    class myclass
    {
      MyString str; //this is fine
    };
    
    //source2.cpp
    #include "myheader"
    
    using namespace std;
    
    class myclass2
    {
      MyString str2; //so is this
    };
    This will work fine, but as I said, you must include this header in all source files which use it. If you want it available globally you can always set it as a forced include. You can do this by going to Project Properties->Configuration Properties->C/C++->Advanced and set Forced Include to this header.
    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://c2kblog.blogspot.com/
    • Marked as answer by Virgo2008 Wednesday, May 19, 2010 3:01 PM
    Wednesday, May 19, 2010 2:29 PM

All replies

  • I think yiou should be able to extend std namespace. I have just tried the below code

    namespace std{
        class MyStd{
        public:
            MyStd(){}
            void foo()
            {   
                OutputDebugString(L"In MyStd Foo\n");
            }
        };
    }

    This I can use like std::MyStd std; in my main.

    • Marked as answer by Virgo2008 Wednesday, May 19, 2010 3:01 PM
    Wednesday, May 19, 2010 6:03 AM
  • You do it as with any namespace, you reopen it. That is, you can always write the following as many times as you want (can't do that with classes):

    namespace X {
    // Put whatever in here.
    }

    However, it is undefined to add names to namespace std except for certain things like template specializations that depend on a user-defined name. Thus, the real question is, "Why do you want to add things to namespace std?" A likely follow-up question is, "Why don't you create your own namespace (outside of std) for you own stuff instead?"


    Doug Harrison (Visual C++ MVP)
    • Proposed as answer by Doug Harrison Wednesday, May 19, 2010 8:51 PM
    Wednesday, May 19, 2010 6:17 AM
  • If I extend namespace std in one file, will it be globally available to my project - which has some dll's?

     

    Wednesday, May 19, 2010 1:08 PM
  • No, like the std namespace is not globally available.

    To get access to anything in that namespace you need to include the appropriate file. So if for example you try

    #include <string>
    
    int main
    {
      std::cout << "doesn't exist"; //will cause an error, this sample is here to show a way to fail
    }

    then it will fail because std::cout doesn't exist at this point even if it is in std. But if you include iostream then it will.

    The same will be true for your application defined functions/classes. If you want them to exist in std then it is easy to extend, but you have to include the header into all source files which want to use it.


    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://c2kblog.blogspot.com/
    Wednesday, May 19, 2010 1:36 PM
  • Is it possible that change in one std is available globally in all files.

    First.cpp

    namespace std

    {

    #define MyString String

    }

    using namespace std;

    class First

    {

    MyString tempSt;

    ....

    }

    --------------------------------------------------------------

    Second.cpp

    using namespace std;

    class Second

    {

    MyString tempSt1;

    ........

     

    }

    Wednesday, May 19, 2010 1:45 PM
  • As I said above, no. The std namespace isn't something which is magically in all files. In fact a standard .cpp file doesn't even have the std namespace until you include one C++ header file.

    What you want to do with that sample above REQUIRES that you put it in a header file snd then include it in all source files which make use of it.

    //source1.cpp
    
    namespace std
    {
      #define MyString string
    }
    
    using namespace std;
    
    class myclass
    {
      MyString str;
    };
    
    //source2.cpp
    
    using namespace std;
    
    class myclass2
    {
      MyString str; //this WILL fail because MyString hasn't been defined.
    };

    This will result in 3 error messages because MyString is undefined in source2.cpp.

    The errors are

    error C2146: syntax error : missing ';' before identifier 'str'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    If however you put it into a header and include that everything will work out fine.

    //myheader
    //remember to prevent multiple inclusion problems by either using #pragma once or
    //by defining a preprocessor definition
    #pragma once
    
    namespace std
    {
      #define MyString string //this is ok, just remember to include <string> before you attempt to use it
    }
    
    //source1.cpp
    #include "myheader"
    
    using namespace std;
    
    class myclass
    {
      MyString str; //this is fine
    };
    
    //source2.cpp
    #include "myheader"
    
    using namespace std;
    
    class myclass2
    {
      MyString str2; //so is this
    };
    This will work fine, but as I said, you must include this header in all source files which use it. If you want it available globally you can always set it as a forced include. You can do this by going to Project Properties->Configuration Properties->C/C++->Advanced and set Forced Include to this header.
    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.
    Visit my (not very good) blog at
    http://c2kblog.blogspot.com/
    • Marked as answer by Virgo2008 Wednesday, May 19, 2010 3:01 PM
    Wednesday, May 19, 2010 2:29 PM
  • namespace std
    {
    #define MyString String
    }

    using namespace std;

    class First
    {
    MyString tempSt;
    }

    --------------------------------------------------------------


    Couple of things:

    1. Macros don't respect scopes, so these two declaration are exactly equivalent:

    namespace std
    {
    #define MyString String
    }

    // In global scope
    #define MyString String

    2. Avoiding using-directives such as "using namespace std;" will help you avoid ambiguities in your code due to possible name collisions between your code and everything in namespace std.

    3. Again, I have to direct you to my original post where I explained why you shouldn't add things to namespace std and asked what you're trying to accomplish.


    Doug Harrison (Visual C++ MVP)
    Wednesday, May 19, 2010 8:57 PM