Wednesday, September 25, 2019

Digest for comp.lang.c++@googlegroups.com - 25 updates in 5 topics

Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 01:02AM -0700

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();
}
Ian Collins <ian-news@hotmail.com>: Sep 25 08:15PM +1200

On 25/09/2019 20:02, Frederick Gotham wrote:
 
> 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
David Brown <david.brown@hesbynett.no>: Sep 25 12:22PM +0200

On 25/09/2019 10:02, Frederick Gotham wrote:
 
> 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.
Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 03:48AM -0700

On Wednesday, September 25, 2019 at 11:22:16 AM UTC+1, David Brown wrote:
> 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. . .
Bo Persson <bo@bo-persson.se>: Sep 25 03:48PM +0200

On 2019-09-25 at 10:02, Frederick Gotham wrote:
 
> ::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
James Kuyper <jameskuyper@alumni.caltech.edu>: Sep 25 10:02AM -0400

On 9/25/19 4:02 AM, Frederick Gotham wrote:
 
> ::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::.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Sep 24 01:50PM -0700

On 9/24/2019 9:33 AM, Bonita Montero wrote:
 
> I've found a solution that is mega-cool. I'm not gonna reveal it
> because I'm thinking about to get an US-patent on it, but it is a
> exotic combination of locked and lock-free programming.
 
Do a deep patent feasibility study! Build a list of as many references
as you can, and read them all. It can safe you some of your hard earned
$$$. Patent lawyers cost a lot money! Or, do you work for Microsoft? For
some reason, I thought about that.
 
Let me guess... Clever hashed locking for the slow-paths, and lock-free
for the fast paths? That is a basic idiom. RCU uses it a lot. Locks for
the writers and sync free for the readers. The readers do not use any
atomics or memory barriers at all. Well, Alpha aside for a moment...
 
Here is a very simple example of a mixture of lock-based and wait-free.
100% wait-free on the push side, and "sometimes" lock-based on the
reader side experimental LIFO:
 
https://groups.google.com/d/msg/comp.arch/8Y0C8zGjtqI/bwg-hBLRAQAJ
 
 
 
> It scales
> almost linear with the number of threads.
 
Cool!
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Sep 24 01:58PM -0700

On 9/24/2019 10:00 AM, Bonita Montero wrote:
>> Hm. That's news, indeed.  Have you produced a formal proof?
 
> There's nothing to prof. The idea isn't very complex,
> but it is very original.
 
It seems like you realized how bad CMPXCHG can be when used in a loop.
Always try XADD and/or XCHG in a loopless algorihtm, before going to a
CAS loop.
 
 
Juha Nieminen <nospam@thanks.invalid>: Sep 25 06:42AM

> because I'm thinking about to get an US-patent on it, but it is a
> exotic combination of locked and lock-free programming. It scales
> almost linear with the number of threads.
 
I don't think you can patent algorithms even in the US currently.
They got rid of that loophole.
Bonita Montero <Bonita.Montero@gmail.com>: Sep 25 09:08AM +0200

> I don't think you can patent algorithms even in the US currently.
> They got rid of that loophole.
 
There's no loophole. You can officially patent algorithms in the US.
David Brown <david.brown@hesbynett.no>: Sep 25 10:10AM +0200

On 25/09/2019 08:42, Juha Nieminen wrote:
>> almost linear with the number of threads.
 
> I don't think you can patent algorithms even in the US currently.
> They got rid of that loophole.
 
You can patent all kinds of nonsense in various countries, with the US
leading in the "give us the money and we'll accept anything" stakes.
 
In particular, in the American system patent offices are funded based on
the number of patents they issue, not on how well they screen
applications or the validity of the patents. The standard is to issue
the patent, and let its validity be tried in court - at vast expense to
the patent holder and the accused patent abuser.
 
I have heard that a rule of thumb is that you should not attempt to
patent unless the idea is worth 7 figures (i.e., over a million
dollars), and you have at least 6 figures of cash in the bank for the
legal expenses of enforcing the patent. And that is assuming you have a
good, solid, valid patent in the first place.
 
I am no lawyer, of course, and I will not be the slightest offended if
someone dismisses this as cynicism or urban legend. (Indeed I'd be
happy if someone can reasonably show the situation is not as bad as I
believe.)
 
(Australia tried a "fast track, low cost" patent system for a while, to
make it easier to get simple patents with less protection than normal
full patents. I believe they stopped after someone used it to patent
the wheel.)
Juha Nieminen <nospam@thanks.invalid>: Sep 25 10:12AM

>> I don't think you can patent algorithms even in the US currently.
>> They got rid of that loophole.
 
> There's no loophole. You can officially patent algorithms in the US.
 
"Allvoice Developments US, LLC v. Microsoft Corp., 612 F. App'x 1009
(Fed. Cir. 2015). The Supreme Court had held previously that software
in algorithm form without machine implementation could not be patented
in process format, see Gottschalk v. Benson and Parker v. Flook, but
could be patented when claimed as a machine inventively using software,
see Diamond v. Diehr."
 
https://en.wikipedia.org/wiki/Software_patents_under_United_States_patent_law
Bonita Montero <Bonita.Montero@gmail.com>: Sep 25 01:37PM +0200

> in process format, see Gottschalk v. Benson and Parker v. Flook, but
> could be patented when claimed as a machine inventively using software,
> see Diamond v. Diehr."
 
Look, here, that's a similar patent to what we're talking about:
https://patents.google.com/patent/US5778442
scott@slp53.sl.home (Scott Lurndal): Sep 25 12:55PM

Bonita Montero <Bonita.Montero@gmail.com> stubbornly removed Juha Nieminen <nospam@thanks.invalid> attribution
 
>> see Diamond v. Diehr."
 
>Look, here, that's a similar patent to what we're talking about:
>https://patents.google.com/patent/US5778442
 
My most recent patent (Issued 9/27/2019) is numbered 10,394,730. Your
referenced patent was granted over twenty years ago, long before the
court case Juha has referenced.
Bonita Montero <Bonita.Montero@gmail.com>: Sep 25 03:28PM +0200


> My most recent patent (Issued 9/27/2019) is numbered 10,394,730.
> Your referenced patent was granted over twenty years ago, long
> before the court case Juha has referenced.
 
Yes, but concrete implementations of algorithms are still
applicable for patents in the US.
queequeg@trust.no1 (Queequeg): Sep 25 11:16AM


> "Effective Modern C++" (Scott Meyers)
 
Thanks! I found it online.
 
--
https://www.youtube.com/watch?v=9lSzL1DqQn0
Juha Nieminen <nospam@thanks.invalid>: Sep 25 01:16PM

> Manfred <noname@add.invalid> wrote:
 
>> "Effective Modern C++" (Scott Meyers)
 
> Thanks! I found it online.
 
It's funny how casually people just admit to their illegal piracy of
intellectual property.
Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 12:48AM -0700

On Tuesday, September 24, 2019 at 6:55:40 PM UTC+1, Paavo Helde wrote:
 
> );
 
> SomeFunc();
> }
 
 
This is my favourite solution so far, thank you Paavo.
 
I'd make it pretty like this:
 
#include <deque>
#include <functional>
 
int GetHandle(void) { return 42; }
void ReleaseHandle(int) { /* Whatever */ }
 
void atexit_ftr(std::function<void()> &&f)
{
/* If this function is never called then the following container is never created */

static std::deque< std::function<void()> > stuff_to_do;

static bool first_time = true;

if ( first_time )
{
first_time = false;

atexit(

[]()
{
for (auto &functor : stuff_to_do)
{
functor();
}
}

);
}

stuff_to_do.push_back(f);
}
 
void SomeFunc(void)
{
int h = GetHandle();

atexit_ftr( [h](){ReleaseHandle(h);} );
}
 
int main(void)
{
SomeFunc();
}
 
This will be very useful in my current project (embedded Linux for a product running a dozen or two services simultaneously that are constantly grabbing and releasing resources -- it's very important that any given service releases all handles when it dies).
Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 12:51AM -0700

On Wednesday, September 25, 2019 at 8:48:38 AM UTC+1, Frederick Gotham wrote:
 
> {
> SomeFunc();
> }
 
 
Two more things:
 
(1) #include <cstdlib>
(2) Write the namespace "std" before atexit, i.e. "std::atexit"
Paavo Helde <myfirstname@osa.pri.ee>: Sep 25 11:49AM +0300

On 25.09.2019 10:48, Frederick Gotham wrote:
 
 
> This will be very useful in my current project (embedded Linux for a product running a dozen or two services simultaneously that are constantly grabbing and releasing resources -- it's very important that any given service releases all handles when it dies).
 
atexit() functions are not called when the process dies because of a
signal like SIGTERM or SIGPIPE; these need special handling.
 
In Linux all file descriptors are closed automatically when the process
dies because of any reasons, so special release is needed only for
resources which do not follow the standard "everything is a file"
design. Or is this different in embedded world?
Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 02:19AM -0700

On Wednesday, September 25, 2019 at 9:49:56 AM UTC+1, Paavo Helde wrote:
> On 25.09.2019 10:48, Frederick Gotham wrote:
 
> atexit() functions are not called when the process dies because of a
> signal like SIGTERM or SIGPIPE; these need special handling.
 
 
I think I can use 'sigaction' to nominate a callback function for these signals, and then inside the callback function I call "std::exit".
 

> dies because of any reasons, so special release is needed only for
> resources which do not follow the standard "everything is a file"
> design. Or is this different in embedded world?
 
 
I use a Text To Speech engine in an embedded device, and you have to get a handle to the engine, then a handle to the voice, then the engine creates a buffer internally. From reading the documentation, they seem quite adamant about you releasing everything before your program ends. To be honest I could probably just contact them and ask if everything gets released when a program dies. The documentation is written by a person who has English as a second language. There are other problems with the documentation... for example typographical errors in the sample program.
Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 02:37AM -0700

On Tuesday, September 24, 2019 at 6:55:40 PM UTC+1, Paavo Helde wrote:
 
> static std::deque<std::function<void()>> globalRegistry;
 
> for (auto& functor : globalRegistry)
 
 
By the way, I see just now that atexit doesn't work FIFO. The last function you give it is the first to be called. So it works LIFO.
Frederick Gotham <cauldwell.thomas@gmail.com>: Sep 25 06:14AM -0700

On Wednesday, September 25, 2019 at 8:51:10 AM UTC+1, Frederick Gotham wrote:
 
> > atexit_ftr( [h](){ReleaseHandle(h);} );
 
Actually this way of doing it will screw up the order of the execution of functions that have been registered with atexit (i.e. All of the functions registered with atexit_ftr will be executed in a sequence, instead of being interspersed with functions registered with atexit).
legalize+jeeves@mail.xmission.com (Richard): Sep 25 04:20AM

[Please do not mail me a copy of your followup]
 
I'm not talking about printing numbers in base 2.
 
I have a program that outputs ESC sequences for a terminal. I don't
want Windows to translate '\n' into '\r\n'. If I construct an
ofstream with std::ios_base::binary, then that's what I get. However,
if I try to use std::cout, I always get a stream that's been opened in
text mode and I get \n -> \r\n in the output.
 
Is there a way to switch std::cout to behave as if I did
std::ofstream(fileName, std::ios_base::binary)?
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <http://terminals-wiki.org>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Paavo Helde <myfirstname@osa.pri.ee>: Sep 25 09:16AM +0300

On 25.09.2019 7:20, Richard wrote:
> text mode and I get \n -> \r\n in the output.
 
> Is there a way to switch std::cout to behave as if I did
> std::ofstream(fileName, std::ios_base::binary)?
 
Sure, this should do the trick:
 
#include <iostream>
#include <io.h>
#include <fcntl.h>
 
int main() {
fflush(stdout);
_setmode(_fileno(stdout), _O_BINARY);
std::cout << "\n";
}
 
This solution is Windows/MSVC-specific, but so is the problem.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: