Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #68641 > unrolled thread

Putting two colons before std -- is it ever needed?

Started byFrederick Gotham <cauldwell.thomas@gmail.com>
First post2019-09-25 01:02 -0700
Last post2019-09-27 20:19 +0000
Articles 16 — 9 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#68641 — Putting two colons before std -- is it ever needed?

FromFrederick Gotham <cauldwell.thomas@gmail.com>
Date2019-09-25 01:02 -0700
SubjectPutting two colons before std -- is it ever needed?
Message-ID<a23612ee-39c5-4b1b-991a-212689fdbfaf@googlegroups.com>
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();
}



[toc] | [next] | [standalone]


#68643

FromIan Collins <ian-news@hotmail.com>
Date2019-09-25 20:15 +1200
Message-ID<gv0m18FldqU3@mid.individual.net>
In reply to#68641
On 25/09/2019 20:02, 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'?

In theory, maybe. In practice, no never.

-- 
Ian

[toc] | [prev] | [next] | [standalone]


#68648

FromDavid Brown <david.brown@hesbynett.no>
Date2019-09-25 12:22 +0200
Message-ID<qmff4b$7v3$1@dont-email.me>
In reply to#68641
On 25/09/2019 10:02, 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'?
> 

I believe it is possible to cause conflicts with "std" that could be
avoided by using "::std".  But it would have to be malicious and
carefully crafted, or totally inept and highly unlucky.  Since there are
few programmers in either category, and if you have either type in your
team then you have far bigger problems than can be solved with "::std",
it is basically a non-issue.

Some people in this group use it, but IMHO it is more about making the
code /look/ like it is super-robust, than actually being good practice.
 The drop in readability is not justified by the miniscule improvement
in robustness or portability.

[toc] | [prev] | [next] | [standalone]


#68649

FromFrederick Gotham <cauldwell.thomas@gmail.com>
Date2019-09-25 03:48 -0700
Message-ID<3924a633-98a6-414a-9691-8ca5af06e9c6@googlegroups.com>
In reply to#68648
On Wednesday, September 25, 2019 at 11:22:16 AM UTC+1, David Brown wrote:
>
> I believe it is possible to cause conflicts with "std" that could be
> avoided by using "::std".  But it would have to be malicious and
> carefully crafted, or totally inept and highly unlucky.  Since there are
> few programmers in either category, and if you have either type in your
> team then you have far bigger problems than can be solved with "::std",
> it is basically a non-issue.

There are some groups of skilled people, that if you pick a person at random, you're very likely to get a bad one.

I've always thought that computer programmers were such a group of people. What I meant to say is: There are of course some very competent computer programmers, and even some expert ones, but if you pick one at random, you'll probably get a bad one.

It is my personal opinion that this holds true for some professions... I'd name a few but I don't want to get the ball rolling on that one. . .

[toc] | [prev] | [next] | [standalone]


#68656

FromBo Persson <bo@bo-persson.se>
Date2019-09-25 15:48 +0200
Message-ID<gv19hnFj06bU1@mid.individual.net>
In reply to#68641
On 2019-09-25 at 10:02, 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();
> }
> 

It would be a problem if SomeFunc() were also a member of the Monkey 
namespace, and one of your coworkers added his own std::string to it.

It that case it is probably time better spent to have a serious talk 
with your collegue, rather than to fill up the code base with millions 
of :: during the next decades.


  Bo Persson


[toc] | [prev] | [next] | [standalone]


#68701

FromVir Campestris <vir.campestris@invalid.invalid>
Date2019-09-26 21:51 +0100
Message-ID<qmj8df$9fi$4@dont-email.me>
In reply to#68656
On 25/09/2019 14:48, Bo Persson wrote:
> 
> It would be a problem if SomeFunc() were also a member of the Monkey 
> namespace, and one of your coworkers added his own std::string to it.
> 
> It that case it is probably time better spent to have a serious talk 
> with your collegue, rather than to fill up the code base with millions 
> of :: during the next decades.

Well put.

There's an explicit prohibition of calling anything std in our coding 
standards.

Andy

[toc] | [prev] | [next] | [standalone]


#68657

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2019-09-25 10:02 -0400
Message-ID<qmfs28$k6b$1@dont-email.me>
In reply to#68641
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::.

[toc] | [prev] | [next] | [standalone]


#68658

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2019-09-25 10:11 -0400
Message-ID<qmfsia$n8n$1@dont-email.me>
In reply to#68657
On 9/25/19 10:02 AM, James Kuyper wrote:
...
> #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;
> }

I made a mistake while creating that code, and after correcting that
mistake I forgot that it invalidated some of my earlier test results.
func() doesn't need to be defined inside ::kuyper::std; it's sufficient
if it's declared inside ::kuyper.

[toc] | [prev] | [next] | [standalone]


#68659

FromFrederick Gotham <cauldwell.thomas@gmail.com>
Date2019-09-25 07:17 -0700
Message-ID<be0ba12b-0ef4-40fb-a190-5f25aa55d358@googlegroups.com>
In reply to#68657
On Wednesday, September 25, 2019 at 3:03:00 PM UTC+1, James Kuyper wrote:

> #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;
> }



It just occurred to me that it might make sense to deliberately leave out the two colons before 'std' so that you can at a later date pull off a little hack like this.

[toc] | [prev] | [next] | [standalone]


#68664

FromStuart Redmann <DerTopper@web.de>
Date2019-09-25 19:51 +0200
Message-ID<2035067947.591125618.518689.DerTopper-web.de@news.eternal-september.org>
In reply to#68641
Frederick Gotham <cauldwell.thomas@gmail.com> 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();
> }
> 

The majority of the readers of this group consider the additional :: as
unnecessary noise since every coding guideline should forbid to define your
own namespace std (inside one of your own namespaces; adding stuff to the
global namespace std is AFAIK forbidden by the standard). 

However, a nested namespace std could be the result of auto-generated code,
for example from some ORM framework. In that case I'd assume that the
auto-generated code always uses fully qualified identifiers if there could
be the slightest chance of clashes. For example, if there is a persistence
module "std" containing an entity called "sin", it could clash with
::std::sin. So if the auto-generated code needed to call ::std::sin, it
should do so by using a full qualification. 

Hand-written code should never need the additional qualification.

Regards,
Stuart

[toc] | [prev] | [next] | [standalone]


#68669

FromJames Kuyper <jameskuyper@alumni.caltech.edu>
Date2019-09-25 15:37 -0400
Message-ID<qmgfml$jsj$1@dont-email.me>
In reply to#68664
On 9/25/19 1:51 PM, Stuart Redmann wrote:
...
> ... adding stuff to the
> global namespace std is AFAIK forbidden by the standard).

The relevant rule is a little more complicated than a simple prohibition:

"The behavior of a C ++ program is undefined if it adds declarations or
definitions to namespace std or to a namespace within namespace std
unless otherwise specified. A program may add a template specialization
for any standard library template to namespace std only if the
declaration depends on a user-defined type and the specialization meets
the standard library requirements for the original template and is not
explicitly prohibited." (20.5.4.2.1p1).

[toc] | [prev] | [next] | [standalone]


#68686

FromFrederick Gotham <cauldwell.thomas@gmail.com>
Date2019-09-26 00:01 -0700
Message-ID<40f36f28-cc1a-434d-84ac-c1f22549737e@googlegroups.com>
In reply to#68664
On Wednesday, September 25, 2019 at 6:51:11 PM UTC+1, Stuart Redmann wrote:

> The majority of the readers of this group consider the additional :: as
> unnecessary noise since every coding guideline should forbid to define your
> own namespace std (inside one of your own namespaces; adding stuff to the
> global namespace std is AFAIK forbidden by the standard). 
> 
> However, a nested namespace std could be the result of auto-generated code,
> for example from some ORM framework. In that case I'd assume that the
> auto-generated code always uses fully qualified identifiers if there could
> be the slightest chance of clashes. For example, if there is a persistence
> module "std" containing an entity called "sin", it could clash with
> ::std::sin. So if the auto-generated code needed to call ::std::sin, it
> should do so by using a full qualification. 
> 
> Hand-written code should never need the additional qualification.


Shouldn't the auto-generated code just simply use prefixes/suffixes to avoid this, e.g. "_autogen__std" instead of "std"?

[toc] | [prev] | [next] | [standalone]


#68708

Fromwoodbrian77@gmail.com
Date2019-09-27 08:50 -0700
Message-ID<7d144dc7-090a-4766-803b-bf49d4be3626@googlegroups.com>
In reply to#68641
On Wednesday, September 25, 2019 at 3:02:13 AM UTC-5, 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();
> }

It's an easy step to take to improve the safety of your code.  
In this 29 line program:
https://github.com/Ebenezer-group/onwards/blob/master/src/cmw/tiers/genz.cc

there's one ::std.

In this 228 line program:
https://github.com/Ebenezer-group/onwards/blob/master/src/cmw/tiers/cmwA.cc

there are 10 uses of ::std.    If I remember right Gabriel Dos Reis
is also a proponent.  Maybe Howard Hinnant also.  It's kind of
like "east const".  



Brian
Ebenezer Enterprises "We few, we happy few.  We band of brothers."
St. Crispin's Day

http://webEbenezer.net

[toc] | [prev] | [next] | [standalone]


#68711

Fromscott@slp53.sl.home (Scott Lurndal)
Date2019-09-27 16:25 +0000
Message-ID<4KqjF.23$kV2.2@fx22.iad>
In reply to#68708
woodbrian77@gmail.com writes:
>On Wednesday, September 25, 2019 at 3:02:13 AM UTC-5, Frederick Gotham wrote:

>> So the question I ask is if there is **ever** any situation when it's necessary to put two colons before 'std'?
>> 

>
>It's an easy step to take to improve the safety of your code.  

It has absolutely nothing to do with code safety.

>In this 29 line program:

Snip blatent product placement.   You haven't justified your statement about
code safety.

[toc] | [prev] | [next] | [standalone]


#68714

Fromwoodbrian77@gmail.com
Date2019-09-27 11:40 -0700
Message-ID<f79c2d54-aaf7-4413-836e-207bfe11170e@googlegroups.com>
In reply to#68711
On Friday, September 27, 2019 at 11:25:46 AM UTC-5, Scott Lurndal wrote:
> woodbrian77@gmail.com writes:
> >On Wednesday, September 25, 2019 at 3:02:13 AM UTC-5, Frederick Gotham wrote:
> 
> >> So the question I ask is if there is **ever** any situation when it's necessary to put two colons before 'std'?
> >> 
> 
> >
> >It's an easy step to take to improve the safety of your code.  
> 
> It has absolutely nothing to do with code safety.
> 
> >In this 29 line program:
> 
> Snip blatent product placement.   

I think of it as a service not a product.
Products in my experience aren't free.


Brian

[toc] | [prev] | [next] | [standalone]


#68717

Fromscott@slp53.sl.home (Scott Lurndal)
Date2019-09-27 20:19 +0000
Message-ID<C9ujF.4733$iJ2.453@fx47.iad>
In reply to#68714
woodbrian77@gmail.com writes:
>On Friday, September 27, 2019 at 11:25:46 AM UTC-5, Scott Lurndal wrote:
>> woodbrian77@gmail.com writes:
>> >On Wednesday, September 25, 2019 at 3:02:13 AM UTC-5, Frederick Gotham wrote:
>> 
>> >> So the question I ask is if there is **ever** any situation when it's necessary to put two colons before 'std'?
>> >> 
>> 
>> >
>> >It's an easy step to take to improve the safety of your code.  
>> 
>> It has absolutely nothing to do with code safety.
>> 
>> >In this 29 line program:
>> 
>> Snip blatent product placement.   
>
>I think of it as a service not a product.
>Products in my experience aren't free.

Neither are useful services.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web