Using fully qualified name for std namespace in C++












6















If name in C++ is not fully qualified, e.g. std::cout, it can lead to an unintentional error, such as mentioned at https://en.cppreference.com/w/cpp/language/qualified_lookup. But using a fully qualified name for ::std namespace, e.q. ::std::cout, is very rare, as I have noticed.



Is there any reason why a fully qualified name for ::std namespace is not used?



And what about using fully qualified name for own created namespaces? Is it good idea?










share|improve this question




















  • 5





    That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.

    – n.m.
    4 hours ago













  • @n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)

    – Christophe
    4 hours ago











  • @n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?

    – Red.Wave
    3 hours ago













  • @Red.Wave It's just a joke.

    – n.m.
    3 hours ago











  • @n.m. It is more. Because it has a point, and actually hits the center point.

    – Red.Wave
    3 hours ago
















6















If name in C++ is not fully qualified, e.g. std::cout, it can lead to an unintentional error, such as mentioned at https://en.cppreference.com/w/cpp/language/qualified_lookup. But using a fully qualified name for ::std namespace, e.q. ::std::cout, is very rare, as I have noticed.



Is there any reason why a fully qualified name for ::std namespace is not used?



And what about using fully qualified name for own created namespaces? Is it good idea?










share|improve this question




















  • 5





    That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.

    – n.m.
    4 hours ago













  • @n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)

    – Christophe
    4 hours ago











  • @n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?

    – Red.Wave
    3 hours ago













  • @Red.Wave It's just a joke.

    – n.m.
    3 hours ago











  • @n.m. It is more. Because it has a point, and actually hits the center point.

    – Red.Wave
    3 hours ago














6












6








6








If name in C++ is not fully qualified, e.g. std::cout, it can lead to an unintentional error, such as mentioned at https://en.cppreference.com/w/cpp/language/qualified_lookup. But using a fully qualified name for ::std namespace, e.q. ::std::cout, is very rare, as I have noticed.



Is there any reason why a fully qualified name for ::std namespace is not used?



And what about using fully qualified name for own created namespaces? Is it good idea?










share|improve this question
















If name in C++ is not fully qualified, e.g. std::cout, it can lead to an unintentional error, such as mentioned at https://en.cppreference.com/w/cpp/language/qualified_lookup. But using a fully qualified name for ::std namespace, e.q. ::std::cout, is very rare, as I have noticed.



Is there any reason why a fully qualified name for ::std namespace is not used?



And what about using fully qualified name for own created namespaces? Is it good idea?







c++ namespaces naming name-lookup scope-resolution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago









Christophe

39.7k43576




39.7k43576










asked 5 hours ago









KamKam

392




392








  • 5





    That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.

    – n.m.
    4 hours ago













  • @n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)

    – Christophe
    4 hours ago











  • @n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?

    – Red.Wave
    3 hours ago













  • @Red.Wave It's just a joke.

    – n.m.
    3 hours ago











  • @n.m. It is more. Because it has a point, and actually hits the center point.

    – Red.Wave
    3 hours ago














  • 5





    That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.

    – n.m.
    4 hours ago













  • @n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)

    – Christophe
    4 hours ago











  • @n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?

    – Red.Wave
    3 hours ago













  • @Red.Wave It's just a joke.

    – n.m.
    3 hours ago











  • @n.m. It is more. Because it has a point, and actually hits the center point.

    – Red.Wave
    3 hours ago








5




5





That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.

– n.m.
4 hours ago







That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.

– n.m.
4 hours ago















@n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)

– Christophe
4 hours ago





@n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)

– Christophe
4 hours ago













@n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?

– Red.Wave
3 hours ago







@n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?

– Red.Wave
3 hours ago















@Red.Wave It's just a joke.

– n.m.
3 hours ago





@Red.Wave It's just a joke.

– n.m.
3 hours ago













@n.m. It is more. Because it has a point, and actually hits the center point.

– Red.Wave
3 hours ago





@n.m. It is more. Because it has a point, and actually hits the center point.

– Red.Wave
3 hours ago












2 Answers
2






active

oldest

votes


















6














You are completely right, in the sense that yyyy::xxx can be ambiguous if there is a namespace yyyy and also a class yyyy which are both visible in the same scope. In this case only the full qualification ::yyyy::xxx can solve the ambiguity. The example of your link makes it very clear:



// from cppreference.com
#include <iostream>
int main() {
struct std{};
std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
::std::cout << "okn"; // OK: ::std finds the namespace std
}


But in practice, it's difficult to create a conflicting std at top level, since most of the includes from the standard library will make it fail:



#include <iostream>

struct std { // OUCH: error: ‘struct std’ redeclared as different kind of symbol
int hello;
};


This means that to create a conflict, you'd need to define local classes or introduce a using clause in another namespace. In addition, nobody will (dare to) call a class std.



Finally, in practice, ::yyyy::xxx is less convenient to read. All this explains why you won't find it very often.



Additional remark



The problem is not so much for std which is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to :::yyyy to disambiguate:



namespace foo {
void printf() { }
}
int main() {
foo::printf(); // ok, namespace is chose because no ambiguity
struct foo {/*...*/ }; // creates ambiguity
//foo::printf(); // error because struct foo is chosen by name lookup
::foo::printf(); // ok, but not if you decide to move the code to be nested in another namespace
namespace mylib = foo ; // or ::foo (see discussion below)
mylib::printf(); // full flexibility :-)
}


Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace ::foo would be used.






share|improve this answer





















  • 1





    namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

    – Red.Wave
    3 hours ago













  • @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

    – Christophe
    3 hours ago











  • I don't mean to bother, but my point is that the provided answer needs a revision.

    – Red.Wave
    3 hours ago



















0














To maintain big code or better readability or clashes in names, C++ has provided namespace " a declarative region".
A namespace definition can appear only at global scope, or nested within another namespace.



#Sample Code
#include <iostream>
int main()
{
struct std{};
std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
::std::cout << "okn"; // OK: ::std finds the namespace std
}


In the above code compiler is looking for cout in struct std , but in next line when you use ::std::cout it looks for cout in globally defined std class.



Solution:



#include <iostream>
//using namespace std; // using keyword allows you to import an entire namespace at once.

namespace test
{
void cout(std::string str)
{
::std::cout<<str;
}
}

int main()
{
cout("Hello");//'cout' was not declared in this scope
::test::cout("Helloo ") ;
::std::cout<<"it is also okn";
}


Or use the in this way , it is just for better readability



##
using namespace test;
int main()
{
cout("Hello");//'cout' was not declared in this scope
cout("Helloo ") ;
::std::cout<<"it is also okn";
}





share|improve this answer








New contributor




Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54732090%2fusing-fully-qualified-name-for-std-namespace-in-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You are completely right, in the sense that yyyy::xxx can be ambiguous if there is a namespace yyyy and also a class yyyy which are both visible in the same scope. In this case only the full qualification ::yyyy::xxx can solve the ambiguity. The example of your link makes it very clear:



    // from cppreference.com
    #include <iostream>
    int main() {
    struct std{};
    std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
    ::std::cout << "okn"; // OK: ::std finds the namespace std
    }


    But in practice, it's difficult to create a conflicting std at top level, since most of the includes from the standard library will make it fail:



    #include <iostream>

    struct std { // OUCH: error: ‘struct std’ redeclared as different kind of symbol
    int hello;
    };


    This means that to create a conflict, you'd need to define local classes or introduce a using clause in another namespace. In addition, nobody will (dare to) call a class std.



    Finally, in practice, ::yyyy::xxx is less convenient to read. All this explains why you won't find it very often.



    Additional remark



    The problem is not so much for std which is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to :::yyyy to disambiguate:



    namespace foo {
    void printf() { }
    }
    int main() {
    foo::printf(); // ok, namespace is chose because no ambiguity
    struct foo {/*...*/ }; // creates ambiguity
    //foo::printf(); // error because struct foo is chosen by name lookup
    ::foo::printf(); // ok, but not if you decide to move the code to be nested in another namespace
    namespace mylib = foo ; // or ::foo (see discussion below)
    mylib::printf(); // full flexibility :-)
    }


    Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace ::foo would be used.






    share|improve this answer





















    • 1





      namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

      – Red.Wave
      3 hours ago













    • @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

      – Christophe
      3 hours ago











    • I don't mean to bother, but my point is that the provided answer needs a revision.

      – Red.Wave
      3 hours ago
















    6














    You are completely right, in the sense that yyyy::xxx can be ambiguous if there is a namespace yyyy and also a class yyyy which are both visible in the same scope. In this case only the full qualification ::yyyy::xxx can solve the ambiguity. The example of your link makes it very clear:



    // from cppreference.com
    #include <iostream>
    int main() {
    struct std{};
    std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
    ::std::cout << "okn"; // OK: ::std finds the namespace std
    }


    But in practice, it's difficult to create a conflicting std at top level, since most of the includes from the standard library will make it fail:



    #include <iostream>

    struct std { // OUCH: error: ‘struct std’ redeclared as different kind of symbol
    int hello;
    };


    This means that to create a conflict, you'd need to define local classes or introduce a using clause in another namespace. In addition, nobody will (dare to) call a class std.



    Finally, in practice, ::yyyy::xxx is less convenient to read. All this explains why you won't find it very often.



    Additional remark



    The problem is not so much for std which is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to :::yyyy to disambiguate:



    namespace foo {
    void printf() { }
    }
    int main() {
    foo::printf(); // ok, namespace is chose because no ambiguity
    struct foo {/*...*/ }; // creates ambiguity
    //foo::printf(); // error because struct foo is chosen by name lookup
    ::foo::printf(); // ok, but not if you decide to move the code to be nested in another namespace
    namespace mylib = foo ; // or ::foo (see discussion below)
    mylib::printf(); // full flexibility :-)
    }


    Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace ::foo would be used.






    share|improve this answer





















    • 1





      namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

      – Red.Wave
      3 hours ago













    • @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

      – Christophe
      3 hours ago











    • I don't mean to bother, but my point is that the provided answer needs a revision.

      – Red.Wave
      3 hours ago














    6












    6








    6







    You are completely right, in the sense that yyyy::xxx can be ambiguous if there is a namespace yyyy and also a class yyyy which are both visible in the same scope. In this case only the full qualification ::yyyy::xxx can solve the ambiguity. The example of your link makes it very clear:



    // from cppreference.com
    #include <iostream>
    int main() {
    struct std{};
    std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
    ::std::cout << "okn"; // OK: ::std finds the namespace std
    }


    But in practice, it's difficult to create a conflicting std at top level, since most of the includes from the standard library will make it fail:



    #include <iostream>

    struct std { // OUCH: error: ‘struct std’ redeclared as different kind of symbol
    int hello;
    };


    This means that to create a conflict, you'd need to define local classes or introduce a using clause in another namespace. In addition, nobody will (dare to) call a class std.



    Finally, in practice, ::yyyy::xxx is less convenient to read. All this explains why you won't find it very often.



    Additional remark



    The problem is not so much for std which is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to :::yyyy to disambiguate:



    namespace foo {
    void printf() { }
    }
    int main() {
    foo::printf(); // ok, namespace is chose because no ambiguity
    struct foo {/*...*/ }; // creates ambiguity
    //foo::printf(); // error because struct foo is chosen by name lookup
    ::foo::printf(); // ok, but not if you decide to move the code to be nested in another namespace
    namespace mylib = foo ; // or ::foo (see discussion below)
    mylib::printf(); // full flexibility :-)
    }


    Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace ::foo would be used.






    share|improve this answer















    You are completely right, in the sense that yyyy::xxx can be ambiguous if there is a namespace yyyy and also a class yyyy which are both visible in the same scope. In this case only the full qualification ::yyyy::xxx can solve the ambiguity. The example of your link makes it very clear:



    // from cppreference.com
    #include <iostream>
    int main() {
    struct std{};
    std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
    ::std::cout << "okn"; // OK: ::std finds the namespace std
    }


    But in practice, it's difficult to create a conflicting std at top level, since most of the includes from the standard library will make it fail:



    #include <iostream>

    struct std { // OUCH: error: ‘struct std’ redeclared as different kind of symbol
    int hello;
    };


    This means that to create a conflict, you'd need to define local classes or introduce a using clause in another namespace. In addition, nobody will (dare to) call a class std.



    Finally, in practice, ::yyyy::xxx is less convenient to read. All this explains why you won't find it very often.



    Additional remark



    The problem is not so much for std which is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to :::yyyy to disambiguate:



    namespace foo {
    void printf() { }
    }
    int main() {
    foo::printf(); // ok, namespace is chose because no ambiguity
    struct foo {/*...*/ }; // creates ambiguity
    //foo::printf(); // error because struct foo is chosen by name lookup
    ::foo::printf(); // ok, but not if you decide to move the code to be nested in another namespace
    namespace mylib = foo ; // or ::foo (see discussion below)
    mylib::printf(); // full flexibility :-)
    }


    Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace ::foo would be used.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 5 mins ago

























    answered 4 hours ago









    ChristopheChristophe

    39.7k43576




    39.7k43576








    • 1





      namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

      – Red.Wave
      3 hours ago













    • @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

      – Christophe
      3 hours ago











    • I don't mean to bother, but my point is that the provided answer needs a revision.

      – Red.Wave
      3 hours ago














    • 1





      namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

      – Red.Wave
      3 hours ago













    • @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

      – Christophe
      3 hours ago











    • I don't mean to bother, but my point is that the provided answer needs a revision.

      – Red.Wave
      3 hours ago








    1




    1





    namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

    – Red.Wave
    3 hours ago







    namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.

    – Red.Wave
    3 hours ago















    @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

    – Christophe
    3 hours ago





    @Red.Wave :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliases

    – Christophe
    3 hours ago













    I don't mean to bother, but my point is that the provided answer needs a revision.

    – Red.Wave
    3 hours ago





    I don't mean to bother, but my point is that the provided answer needs a revision.

    – Red.Wave
    3 hours ago













    0














    To maintain big code or better readability or clashes in names, C++ has provided namespace " a declarative region".
    A namespace definition can appear only at global scope, or nested within another namespace.



    #Sample Code
    #include <iostream>
    int main()
    {
    struct std{};
    std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
    ::std::cout << "okn"; // OK: ::std finds the namespace std
    }


    In the above code compiler is looking for cout in struct std , but in next line when you use ::std::cout it looks for cout in globally defined std class.



    Solution:



    #include <iostream>
    //using namespace std; // using keyword allows you to import an entire namespace at once.

    namespace test
    {
    void cout(std::string str)
    {
    ::std::cout<<str;
    }
    }

    int main()
    {
    cout("Hello");//'cout' was not declared in this scope
    ::test::cout("Helloo ") ;
    ::std::cout<<"it is also okn";
    }


    Or use the in this way , it is just for better readability



    ##
    using namespace test;
    int main()
    {
    cout("Hello");//'cout' was not declared in this scope
    cout("Helloo ") ;
    ::std::cout<<"it is also okn";
    }





    share|improve this answer








    New contributor




    Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      To maintain big code or better readability or clashes in names, C++ has provided namespace " a declarative region".
      A namespace definition can appear only at global scope, or nested within another namespace.



      #Sample Code
      #include <iostream>
      int main()
      {
      struct std{};
      std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
      ::std::cout << "okn"; // OK: ::std finds the namespace std
      }


      In the above code compiler is looking for cout in struct std , but in next line when you use ::std::cout it looks for cout in globally defined std class.



      Solution:



      #include <iostream>
      //using namespace std; // using keyword allows you to import an entire namespace at once.

      namespace test
      {
      void cout(std::string str)
      {
      ::std::cout<<str;
      }
      }

      int main()
      {
      cout("Hello");//'cout' was not declared in this scope
      ::test::cout("Helloo ") ;
      ::std::cout<<"it is also okn";
      }


      Or use the in this way , it is just for better readability



      ##
      using namespace test;
      int main()
      {
      cout("Hello");//'cout' was not declared in this scope
      cout("Helloo ") ;
      ::std::cout<<"it is also okn";
      }





      share|improve this answer








      New contributor




      Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        To maintain big code or better readability or clashes in names, C++ has provided namespace " a declarative region".
        A namespace definition can appear only at global scope, or nested within another namespace.



        #Sample Code
        #include <iostream>
        int main()
        {
        struct std{};
        std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
        ::std::cout << "okn"; // OK: ::std finds the namespace std
        }


        In the above code compiler is looking for cout in struct std , but in next line when you use ::std::cout it looks for cout in globally defined std class.



        Solution:



        #include <iostream>
        //using namespace std; // using keyword allows you to import an entire namespace at once.

        namespace test
        {
        void cout(std::string str)
        {
        ::std::cout<<str;
        }
        }

        int main()
        {
        cout("Hello");//'cout' was not declared in this scope
        ::test::cout("Helloo ") ;
        ::std::cout<<"it is also okn";
        }


        Or use the in this way , it is just for better readability



        ##
        using namespace test;
        int main()
        {
        cout("Hello");//'cout' was not declared in this scope
        cout("Helloo ") ;
        ::std::cout<<"it is also okn";
        }





        share|improve this answer








        New contributor




        Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        To maintain big code or better readability or clashes in names, C++ has provided namespace " a declarative region".
        A namespace definition can appear only at global scope, or nested within another namespace.



        #Sample Code
        #include <iostream>
        int main()
        {
        struct std{};
        std::cout << "failn"; // Error: unqualified lookup for 'std' finds the struct
        ::std::cout << "okn"; // OK: ::std finds the namespace std
        }


        In the above code compiler is looking for cout in struct std , but in next line when you use ::std::cout it looks for cout in globally defined std class.



        Solution:



        #include <iostream>
        //using namespace std; // using keyword allows you to import an entire namespace at once.

        namespace test
        {
        void cout(std::string str)
        {
        ::std::cout<<str;
        }
        }

        int main()
        {
        cout("Hello");//'cout' was not declared in this scope
        ::test::cout("Helloo ") ;
        ::std::cout<<"it is also okn";
        }


        Or use the in this way , it is just for better readability



        ##
        using namespace test;
        int main()
        {
        cout("Hello");//'cout' was not declared in this scope
        cout("Helloo ") ;
        ::std::cout<<"it is also okn";
        }






        share|improve this answer








        New contributor




        Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 hours ago









        Gaurav GGaurav G

        11




        11




        New contributor




        Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Gaurav G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54732090%2fusing-fully-qualified-name-for-std-namespace-in-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Callistus I

            Tabula Rosettana

            How to label and detect the document text images