Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #68657
| From | James Kuyper <jameskuyper@alumni.caltech.edu> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Putting two colons before std -- is it ever needed? |
| Date | 2019-09-25 10:02 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <qmfs28$k6b$1@dont-email.me> (permalink) |
| References | <a23612ee-39c5-4b1b-991a-212689fdbfaf@googlegroups.com> |
On 9/25/19 4:02 AM, Frederick Gotham wrote:
>
> Occasionally I see people write code like this:
>
> #include <string>
>
> int main()
> {
> ::std::string str;
> }
>
> I think you would only ever need to put those two colons before 'std' if you created a class or a namespace called 'std', and of course if you did, then it would have to be inside another namespace (because otherwise you'd have a name clash).
>
> I tried compiling the following code, and each time it created a 'string' from the C++ standard library.
>
> So the question I ask is if there is **ever** any situation when it's necessary to put two colons before 'std'?
>
> #include <string>
> #include <iostream>
>
> namespace Monkey {
>
> class nonstd {
> int i;
> };
>
> class mutant : public nonstd {
> int j;
> };
>
> class mongrel : public nonstd {
> int k;
> };
>
> class std {
> public:
> typedef char *string;
> };
> }
>
> int SomeFunc()
> {
> using namespace Monkey;
>
> std::string a;
>
> ::std::string b;
>
> ::std::cout << sizeof(a) << " " << sizeof(b) << ::std::endl;
> }
>
> int main()
> {
> std::string a;
>
> ::std::string b;
>
> ::std::cout << sizeof(a) << " " << sizeof(b) << ::std::endl;
>
> SomeFunc();
> }
#include <iostream>
#include <cstdio>
namespace kuyper {
namespace std {
void printf(const char *txt) {
::std::cout << "Kuyper::std::" << txt;
}
void func(void)
{
std::printf("printf\n");
::std::printf("::std::printf\n");
}
}
}
int main(void)
{
kuyper::std::func();
return 0;
}
Compiled using g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
with the following command line:
g++ -std=c++1y -pedantic -Wall -Wpointer-arith -Wcast-align
-fno-enforce-eh-specs -ffor-scope -fno-gnu-keywords
-fno-nonansi-builtins -Wctor-dtor-privacy -Wnon-virtual-dtor
-Wold-style-cast -Woverloaded-virtual -Wsign-promo std.cpp -o std
Execution produces the following results:
Kuyper::std::printf
::std::printf
Code where std: is simply ambiguous, and must therefore be replaced by
::std if that's what you want to use is slightly simpler to create, but
is ill-formed. You need well-formed code like that above to make an
alternative namespace std be a preferred match to an unqualified std::.
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Putting two colons before std -- is it ever needed? Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-09-25 01:02 -0700
Re: Putting two colons before std -- is it ever needed? Ian Collins <ian-news@hotmail.com> - 2019-09-25 20:15 +1200
Re: Putting two colons before std -- is it ever needed? David Brown <david.brown@hesbynett.no> - 2019-09-25 12:22 +0200
Re: Putting two colons before std -- is it ever needed? Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-09-25 03:48 -0700
Re: Putting two colons before std -- is it ever needed? Bo Persson <bo@bo-persson.se> - 2019-09-25 15:48 +0200
Re: Putting two colons before std -- is it ever needed? Vir Campestris <vir.campestris@invalid.invalid> - 2019-09-26 21:51 +0100
Re: Putting two colons before std -- is it ever needed? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-25 10:02 -0400
Re: Putting two colons before std -- is it ever needed? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-25 10:11 -0400
Re: Putting two colons before std -- is it ever needed? Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-09-25 07:17 -0700
Re: Putting two colons before std -- is it ever needed? Stuart Redmann <DerTopper@web.de> - 2019-09-25 19:51 +0200
Re: Putting two colons before std -- is it ever needed? James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-09-25 15:37 -0400
Re: Putting two colons before std -- is it ever needed? Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-09-26 00:01 -0700
Re: Putting two colons before std -- is it ever needed? woodbrian77@gmail.com - 2019-09-27 08:50 -0700
Re: Putting two colons before std -- is it ever needed? scott@slp53.sl.home (Scott Lurndal) - 2019-09-27 16:25 +0000
Re: Putting two colons before std -- is it ever needed? woodbrian77@gmail.com - 2019-09-27 11:40 -0700
Re: Putting two colons before std -- is it ever needed? scott@slp53.sl.home (Scott Lurndal) - 2019-09-27 20:19 +0000
csiph-web