Thursday, May 10, 2018

Digest for comp.lang.c++@googlegroups.com - 15 updates in 4 topics

Peng Yu <pengyu.ut@gmail.com>: May 10 12:51PM -0700

Hi,
 
https://github.com/facebookresearch/fastText/blob/master/src/fasttext.cc#L647
 
At the above code, it does not allow stdin.
 
if (args_->input == "-") {
// manage expectations
throw std::invalid_argument("Cannot use stdin for training!");
}
 
The relevant functions are the following. But I don't see which C++ I/O function can not be used with stdin. Does anybody see? Thanks.
 
https://github.com/facebookresearch/fastText/blob/master/src/dictionary.cc#L228
Dictionary::readFromFile
 
https://github.com/facebookresearch/fastText/blob/master/src/dictionary.cc#L201
Dictionary::readWord
 
Regards,
Peng
legalize+jeeves@mail.xmission.com (Richard): May 10 08:12PM

[Please do not mail me a copy of your followup]
 
Peng Yu <pengyu.ut@gmail.com> spake the secret code
> }
 
>The relevant functions are the following. But I don't see which C++ I/O
>function can not be used with stdin. Does anybody see? Thanks.
 
They probably just didn't understand how to deal with the fact that
istream's are not copyable. In this situation, the easiest solution
is to use a pointer:
 
{
std::ifstream ifs;
std::istream *input = nullptr;
if (args_->input == "-")
{
input = &std::cin;
}
else
{
ifs.open(args_->input);
input = &ifs;
}
dict_->readFromFile(*input);
}
 
(If we stick the ifstream in a local scope with {}'s we don't need to
explicitly close it and worry about someone using it later in the
function.)
--
"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>
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 10 10:36PM +0200

On 10.05.2018 22:12, Richard wrote:
 
> (If we stick the ifstream in a local scope with {}'s we don't need to
> explicitly close it and worry about someone using it later in the
> function.)
 
I don't understand the need for pointer?
 
if( args_->input == "-" )
{
dict_->readFromFile( std::cin );
}
else
{
std::ifstream f{ args_->input };
if( f.fail() ) { throw "Baluba!"; }
dict_->readFromFile( f );
}
 
Cheers!,
 
- Alf
legalize+jeeves@mail.xmission.com (Richard): May 10 08:52PM

[Please do not mail me a copy of your followup]
 
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> spake the secret code
> if( f.fail() ) { throw "Baluba!"; }
> dict_->readFromFile( f );
> }
 
One cat, two ways to skin it :-).
--
"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>
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 10 09:29PM +0100

On 09/05/2018 22:32, Richard wrote:
> - std::launder()
> - Pack expansions in using declarations
 
> There's a second feature table for the standard library.
 
Good luck avoiding the std::auto_ptr shit storm.
 
/Flibble
 
--
"Suppose it's all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That's what I would say."
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 10 12:52AM +0200

On 09.05.2018 23:24, David Brown wrote:
 
> You are mistaken :-) (As far as I know - correct me if I'm wrong here).
 
> Concepts add restrictions and limitations, but they won't generate code
> like this.  Metaclasses will, but they are further off in the future.
 
Once seen it's trivial to generate the relational operators via a CRTP
mix-in class.
 
AFAIK that was the original use case for the Barton-Nackman trick.
 
For the language lawyer there's stuff to dig into here, because the
language evolved to completely different rules about "namespace
injection", but in a way that would preserve existing BN-trick code. At
the time of their "Scientific programming" (IIRC) book the operators
were found in the enclosing namespace, while today they're found via
argument dependent lookup.
 
 
Cheers!,
 
- Alf
Rudi Cilibrasi <cilibrar@gmail.com>: May 09 09:21PM -0700

Sutter et al's paper explains the motivation and advantages pretty well for the consistent comparison operator:
 
http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0515r0.pdf
 
Cheers
 
Rudi
Paavo Helde <myfirstname@osa.pri.ee>: May 10 08:01AM +0300

On 10.05.2018 0:24, David Brown wrote:
 
> You are mistaken :-) (As far as I know - correct me if I'm wrong here).
 
> Concepts add restrictions and limitations, but they won't generate code
> like this. Metaclasses will, but they are further off in the future.
 
Right, I must have had metaclasses in mind.
 
While CRTP does work for this task, as Alf mentioned, it would not make
the code any clearer. The proposed metaclasses syntax seems more
straightforward.
boltar@cylonHQ.com: May 10 08:20AM

On Wed, 9 May 2018 16:28:20 +0000 (UTC)
 
>Maybe you would get that impression if all you know is what you read
>on usenet. Try watching the videos on these topics by the very same
>people that are writing the standards proposal.
 
Next time I have insomnia I'll give them a look.
boltar@cylonHQ.com: May 10 08:35AM

On Wed, 09 May 2018 20:29:14 +0300
>> It might be simple, but its also non standard having an operator that
>> implicitly creates overloads for other operators.
 
>If it's in the C++ standard, then it's standard ;-)
 
Well yes, I suppose, but you know what I mean :)
 
>> macro. Thats what they're for.
 
>Nope, macros are a workaround that should be used as little as possible
>in C++.
 
They do the job. In what sense is that a workaround? Plus they're absolutely
essential if you're writing portable code that does more than just Hello World.
 
>would not be much point in using it. However, currently it replaces a
>lot of unneeded boilerplate code related to forwarding all the bound
>variables and maybe also creating a helper class for clarity.
 
I've seen a lot of code with lambda functions in, clarity isn't the word I'd
use for any of it. Code salad maybe.
 
>For curiosity, what kind of job that is? BTW, division of labor was
>invented already many thousands of years ago.
 
A job where the other devs were let go and I'm the only one left. And we also
have no sys admin at the moment. Guess who gets to do that job too? Devops is
the titre du jour for that at the moment. I'd give it another name thats a bit
less polite.
 
>I understand that it is hard or maybe impossible to know all these
>languages in deep, but this is no reason for being proud about your
>ignorance and display it here in this group.
 
Disagreeing with the groupthink is not ignorance. And when the day comes that
I'm unable to write some clean code using what knowledge I have then maybe I'll
dive into the latest standards. Up until now I've barely had any use for
anything from the 2011 standard apart from autos and initialiser lists never
mind beyond.
 
 
>This is because you are not familiar with newer C++ standards. Many
>areas which used to require third-party libraries or OS API-s are now
>part of the C++ standard and can be used in portable code. GUI
 
Not enough to be particularly useful for systems programming, and the C++
standard threading library leaves a lot to be desired compared to pthreads
though I guess its useful for portability. Also they seem to have been aiming
at Windows which prefers threads over multi process (mainly since it can't do
process spawning in any sane way) as AFAIK there's little sign of standard
support for the latter on the horizon.
 
>programming is the main missing part and for that you should use a
>portable third-party library.
 
Learnt Xlib back in the 90s, wrote some games. Even wrote an X window manager
about 15 years ago.
David Brown <david.brown@hesbynett.no>: May 09 11:24PM +0200

On 09/05/18 19:29, Paavo Helde wrote:
 
> Replicating an interface like that will be covered by the C++ concepts
> mechanism if I am not mistaken. If so, the interface-creating effect of
> <==> indeed seems like an unneeded duplication.
 
You are mistaken :-) (As far as I know - correct me if I'm wrong here).
 
Concepts add restrictions and limitations, but they won't generate code
like this. Metaclasses will, but they are further off in the future.
 
Concepts /could/ be used here to some extent. For example, if there
were a <=> operator defined but there was no automatic generation of the
additional comparisons by default, then you could have:
 
template<typename T>
concept Space_strong_ordering = requires(T a, T b) {
{ a <=> b } -> std::strong_ordering;
};
 
template<Space_strong_ordering T>
bool operator<=(const T& a, const T& b)
{ return (a <=> b) <= 0; }
 
// And so on for the other comparisons
 
However, that would mean you would have these comparisons for anything
that supported the spaceship operator, instead of being able to disable
them when you don't want them or easily override them for specialisation.
 
And you still need the spaceship operator itself.
 
 
> would not be much point in using it. However, currently it replaces a
> lot of unneeded boilerplate code related to forwarding all the bound
> variables and maybe also creating a helper class for clarity.
 
Indeed - lambdas are a lot more than just a function pointer. You /can/
get the functionality via functor objects, but as you say there is a lot
of extra code need that lambdas simplify.
 
Programming with lambdas often seems very strange when you are
unfamiliar with them, and for those who don't use them, it can be hard
to understand their benefits. But once you have got the hang of them,
they can be very handy for putting code at the point you want, not
spread out in the source code file.
"K. Frank" <kfrank29.c@gmail.com>: May 10 12:45PM -0700

Hi Boltar!
 
A quick response to your comment about threads -- not about the
original <=> topic:
 
> standard threading library leaves a lot to be desired compared to pthreads
> though I guess its useful for portability.
> ...
 
What do you find that pthreads does for you that is not comparably
straightforward using std::thread, etc?
 
I like pthreads and think it is quite good. (I grew up on pthreads,
so to speak.) But I also like std::thread, and it, too, seems
quite good to me.
 
Sure, std::thread uses classes that you call member functions on,
rather than passing pthread_t's around to free functions, but it
has very much the same flavor as pthreads, and the basic building
blocks of std::thread and pthreads map pretty cleanly to one another.
 
What's std::thread missing that it "leaves a lot to be desired?"
 
 
Happy Multi-Threaded Hacking!
 
 
K. Frank
wyniijj@gmail.com: May 10 08:02AM -0700

Recently I had an idea to reimplement new as a function or template
using C malloc. An application example is to implement 'new *this'.
I don't use std-library, most of my codes are 'home-made', only few are
using std:: things (std::terminate(), std::numeric_limits, <typeinfo> )
So is it doable? Any sugestion will be appreciated.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 10 05:12PM +0200

> Recently I had an idea to reimplement new as a function or template
> using C malloc.
 
Why would you want to do use malloc for C++ `new`?
 
 
> An application example is to implement 'new *this'.
 
That's not valid C++ syntax.
 
So, it's not really an example of anything.
 
 
> I don't use std-library, most of my codes are 'home-made', only few are
> using std:: things (std::terminate(), std::numeric_limits, <typeinfo> )
> So is it doable?
 
Yes, you can override the allocation and deallocation functions,
operator new, operator new[], operator delete and operator delete[], in
the global namespace as well as per class.
 
 
> Any sugestion will be appreciated.
 
I would suggest not doing this. ;-)
 
 
Cheers!,
 
- Alf
Bo Persson <bop@gmb.dk>: May 10 05:49PM +0200

> I don't use std-library, most of my codes are 'home-made', only few are
> using std:: things (std::terminate(), std::numeric_limits, <typeinfo> )
> So is it doable? Any sugestion will be appreciated.
 
Before putting too much work into this, you might want to check what
your compiler does by default.
 
For example, the compiler I'm using does this:
 
 
void* __CRTDECL operator new(size_t const size)
{
for (;;)
{
if (void* const block = malloc(size))
{
return block;
}
 
if (_callnewh(size) == 0)
{
if (size == SIZE_MAX)
{
__scrt_throw_std_bad_array_new_length();
}
else
{
__scrt_throw_std_bad_alloc();
}
}
 
// The new handler was successful; try to allocate again...
}
}
 
 
 
 
Bo Persson
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: