Using fully qualified name for std namespace in C++

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












7















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



















  • 10





    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.
    Feb 17 at 10:41












  • @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
    Feb 17 at 11:08











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

    – Red.Wave
    Feb 17 at 11:21












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

    – n.m.
    Feb 17 at 11:28






  • 1





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

    – Red.Wave
    Feb 17 at 11:29















7















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



















  • 10





    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.
    Feb 17 at 10:41












  • @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
    Feb 17 at 11:08











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

    – Red.Wave
    Feb 17 at 11:21












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

    – n.m.
    Feb 17 at 11:28






  • 1





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

    – Red.Wave
    Feb 17 at 11:29













7












7








7


1






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 Feb 17 at 10:54









Christophe

40.9k43578




40.9k43578










asked Feb 17 at 10:04









KamKam

443




443







  • 10





    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.
    Feb 17 at 10:41












  • @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
    Feb 17 at 11:08











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

    – Red.Wave
    Feb 17 at 11:21












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

    – n.m.
    Feb 17 at 11:28






  • 1





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

    – Red.Wave
    Feb 17 at 11:29












  • 10





    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.
    Feb 17 at 10:41












  • @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
    Feb 17 at 11:08











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

    – Red.Wave
    Feb 17 at 11:21












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

    – n.m.
    Feb 17 at 11:28






  • 1





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

    – Red.Wave
    Feb 17 at 11:29







10




10





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.
Feb 17 at 10:41






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.
Feb 17 at 10:41














@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
Feb 17 at 11:08





@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
Feb 17 at 11:08













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

– Red.Wave
Feb 17 at 11:21






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

– Red.Wave
Feb 17 at 11:21














@Red.Wave It's just a joke.

– n.m.
Feb 17 at 11:28





@Red.Wave It's just a joke.

– n.m.
Feb 17 at 11:28




1




1





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

– Red.Wave
Feb 17 at 11:29





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

– Red.Wave
Feb 17 at 11:29












2 Answers
2






active

oldest

votes


















10














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
    Feb 17 at 11:19












  • @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
    Feb 17 at 11:38











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

    – Red.Wave
    Feb 17 at 11:46


















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























  • Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

    – Christophe
    Feb 17 at 15:12










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









10














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
    Feb 17 at 11:19












  • @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
    Feb 17 at 11:38











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

    – Red.Wave
    Feb 17 at 11:46















10














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
    Feb 17 at 11:19












  • @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
    Feb 17 at 11:38











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

    – Red.Wave
    Feb 17 at 11:46













10












10








10







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 Feb 17 at 15:07

























answered Feb 17 at 10:41









ChristopheChristophe

40.9k43578




40.9k43578







  • 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
    Feb 17 at 11:19












  • @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
    Feb 17 at 11:38











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

    – Red.Wave
    Feb 17 at 11:46












  • 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
    Feb 17 at 11:19












  • @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
    Feb 17 at 11:38











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

    – Red.Wave
    Feb 17 at 11:46







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
Feb 17 at 11:19






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
Feb 17 at 11:19














@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
Feb 17 at 11:38





@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
Feb 17 at 11:38













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

– Red.Wave
Feb 17 at 11:46





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

– Red.Wave
Feb 17 at 11:46













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























  • Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

    – Christophe
    Feb 17 at 15:12















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























  • Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

    – Christophe
    Feb 17 at 15:12













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













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












share|improve this answer



share|improve this answer










answered Feb 17 at 12:55









Gaurav GGaurav G

11




11












  • Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

    – Christophe
    Feb 17 at 15:12

















  • Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

    – Christophe
    Feb 17 at 15:12
















Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

– Christophe
Feb 17 at 15:12





Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.

– Christophe
Feb 17 at 15:12

















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

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)