Sunday, February 5, 2017

Digest for comp.lang.c++@googlegroups.com - 23 updates in 8 topics

bitrex <bitrex@de.lete.earthlink.net>: Feb 05 11:00AM -0500

On 02/02/2017 08:43 AM, Öö Tiib wrote:
 
> Of course if you are not aware of existence of 'boost::variant'
> like you say then it is likely that you can't answer why your
> 'bucket' is special compared to such alternatives.
 
I remember boost::variant being a pain in the butt to use, and
boost::any is really slow!
woodbrian77@gmail.com: Feb 05 11:29AM -0800

On Thursday, February 2, 2017 at 7:43:46 AM UTC-6, Öö Tiib wrote:
> of this letter.
 
> The 'std::variant' will be added to C++ standard library this
> year
 
Howard Hinnant predicted on an episode of Cppchat
 
http://www.slashslash.info/cppchat/
 
that the latest version of the standard would be passed on
December 31, 2017 at 10pm (if I remember correctly).
 
Better late than never ...
 
 
> because standard library classes are generally more efficient,
> flexible, available for more platforms, better tested and/or
> faster fixed than custom classes.
 
I think implementation-wise Clang shuns standard containers.
I try to avoid them also, but I'm not successful yet in avoiding std::queue/std::deque.
 
 
Brian
Ebenezer Enterprises - "Hope is worth any money." Thomas Fuller
http://webEbenezer.net
alexo <alessandro.volturno@libero.it>: Feb 05 11:29AM +0100

Il 04/02/2017 23:43, Richard ha scritto:
 
> By "output garbage", I assume you are referring to the output of the
> program and not the output of the compilation process.
 
You guessed it ;) sorry I was not clear.
 
Compilation with minGW is ok, no warnings.
I've re-compiled your code in Visual studio and it compiles perfectly.
I don't know what went wrong that time.
 
But, as already reported earlier, this is what I get when running your code:
 
[2J
[0;0f
[0;31mH [0;32me [0;33ml [0;34ml [0;35mo [0;36m, [0;37m
[0;31mw [0;32mo [0;33mr [0;34ml [0;35md [0;36m. [0;37m. [0;31m. [0;32m
[10;30f:)...
 
> The Windows command processor CMD.EXE doesn't guarantee to support
> ANSI escape sequences.
 
OK, nor even Window 10's CMD supports it as I can see.
 
> There are several choices here. You can code something that is
> portable across operating systems or you can code something specific
> to Windows.
 
The solution using the macros _WIN32 or __linux__ worked fine.
I've tested the code on both the OSes and calling the system() routine
worked perfectly.
 
The most portable solution is to use the curses library
> and have it output the appropriate escape codes for the environment.
 
PDcurses is what I'm studying, but I don't have the necessity to manage
the console screen as individual cells. It's just fine the normal cout.
I just needed a way to clear the screen's buffer.
 
The game I'm trying to write is a text-driven old-style RPG. It needs a
lot of paper to sketch the corridor passages and to annote the eventual
content of a room. But this project is just a hobby, that I write from
time to time not even frequently. Probably will be never finished.
 
> To code directly to Windows you can use one of the two approaches
> shown here:
> <https://msdn.microsoft.com/en-us/library/windows/desktop/ms682022(v=vs.85).aspx>
 
The second solution require to include the <windows.h> header. I don't
know anything about it, sorry. The first olution is the simpler and
clearer and will be that I will use.
Programming is an "art"
...and art is beauty.
 
So the principle I conform is KISS: "keep it simple, stuid" or "keep it
short, simple".
 
Regards,
Alex
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 05 12:07PM +0100

On 05.02.2017 11:29, alexo wrote:
 
>> The Windows command processor CMD.EXE doesn't guarantee to support
>> ANSI escape sequences.
 
> OK, nor even Window 10's CMD supports it as I can see.
 
Windows 10's console windows support ANSI escape sequences via the
ordinary standard streams, but not via direct console i/o.
 
However, since it's a new feature as of Windows 10, expect some
imperfections.
 
Cheers & hth.,
 
- Alf
alexo <alessandro.volturno@libero.it>: Feb 05 02:02PM +0100

Il 05/02/2017 12:07, Alf P. Steinbach ha scritto:
 
>> OK, nor even Window 10's CMD supports it as I can see.
 
> Windows 10's console windows support ANSI escape sequences via the
> ordinary standard streams, but not via direct console i/o.
 
can you explain it with a simple example?
sorry but I'm not that geek as you think of me ; )
 
thanks,
alf.p.steinbach@gmail.com: Feb 05 05:20AM -0800

On Sunday, February 5, 2017 at 2:02:50 PM UTC+1, alexo wrote:
> > ordinary standard streams, but not via direct console i/o.
 
> can you explain it with a simple example?
> sorry but I'm not that geek as you think of me ; )
 
In a cmd.exe instance, type "echo " followed by Alt+027 on the numeric keypad, followed by "[2J".
 
The Alt+027 is echoed as "^[" but is internally the ASCII escape character:
 
[example]
[C:\my\drive_h\forums\clc++\001]
> echo ^[[2J
[/example]
 
Now when you hit return the specified text is output by the echo command, causing the screen to be cleared. The cursor maintains its position so it's not exactly the same as the cls command. But shows that escape sequences work.
 
When you try that directly in a C++ program you get silly translated (interpreted) characters instead of a clear screen. That's because with both recent g++ and recent Visual C++ -- I don't know how it was before -- the newgfangled escape sequence support is disabled by default.
 
To enable it you can call the Windows API SetConsoleMode function:
 
[code]
#include <windows.h>
// Documentation of Windows support for ANSI escape sequences:
// https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
 
#include <windows.h>
namespace winapi {
HANDLE const out = GetStdHandle( STD_OUTPUT_HANDLE );
 
void enable_ansi_sequences()
{
DWORD old_mode;
GetConsoleMode( winapi::out, &old_mode );
SetConsoleMode(
winapi::out,
old_mode | 4 // ENABLE_VIRTUAL_TERMINAL_PROCESSING
);
}
}

#include <iostream>
using namespace std;
int main()
{
winapi::enable_ansi_sequences();
cout << "\x1B[2J"; // Clear the screen.
// cout << "\x1B[H"; // And send cursor hoome (up left).
}
[/code]
 
You can place that Windows-specific stuff, plus the call to it, in a separate compilation unit, then the main code can be the same for Windows and Unix-land.
 
As long as only the common subset of escape sequences is used. :)
 
 
Cheers & hth.,
 
- Alf
alexo <alessandro.volturno@libero.it>: Feb 05 05:23PM +0100

> [/example]
 
> Now when you hit return the specified text is output by the echo command, causing the screen to be cleared. The cursor maintains its position so it's not exactly the same as the cls command. But shows that escape sequences work.
 
> When you try that directly in a C++ program you get silly translated (interpreted) characters instead of a clear screen. That's because with both recent g++ and recent Visual C++ -- I don't know how it was before -- the newgfangled escape sequence support is disabled by default.
 
tested. OK.
 
> // cout << "\x1B[H"; // And send cursor hoome (up left).
> }
> [/code]
 
not tested yet. But I find it terribly ugly. It's simpler to have a
single call to system() that in windows calls "cls" and on linux calls
"reset".
What is coming out from this conversation is a "new world" of concepts.
I don't want study it, but thank you to have me shown this possibility.
Computers are woderful.
 
thank you
legalize+jeeves@mail.xmission.com (Richard): Feb 05 06:54PM

[Please do not mail me a copy of your followup]
 
You didn't answer my question about the compile error.
 
That's the only interesting part of this thread to me.
--
"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>
Jorgen Grahn <grahn+nntp@snipabacken.se>: Feb 05 10:21AM

On Wed, 2017-01-11, JiiPee wrote:
> but other ways. What would be the best way for the function to report
> about "dividing by zero" case? How would you do it?
 
> I kind of agree with Bjarne here.... do you?
 
I'd just document that average() has undefined behavior if you feed it
the empty set.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
JiiPee <no@notvalid.com>: Feb 05 01:33PM

On 05/02/2017 10:21, Jorgen Grahn wrote:
> I'd just document that average() has undefined behavior if you feed it
> the empty set.
 
 
I had an interview at the central of London. And after i finished the
avarage() function, he asked "thats all?" (I did not mention any error
handling). then he said: "..and maybe to throw an exception ?". So he
seemed to see that a good one..
JiiPee <no@notvalid.com>: Feb 05 01:34PM

On 05/02/2017 10:21, Jorgen Grahn wrote:
> I'd just document that average() has undefined behavior if you feed it
> the empty set.
 
 
maybe in a release version, but surely in a debug version an assert()
would be a good there...
Jorgen Grahn <grahn+nntp@snipabacken.se>: Feb 05 06:51PM

On Sun, 2017-02-05, JiiPee wrote:
>> the empty set.
 
> maybe in a release version, but surely in a debug version an assert()
> would be a good there...
 
The documentation would be the same in both cases.
 
Yes, maybe an assertion ... although the difference between an abort()
and division by zero is slight.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
woodbrian77@gmail.com: Feb 05 10:11AM -0800

Has anyone gotten CodeChecker working on FreeBSD?
https://github.com/Ericsson/CodeChecker
 
The "make venv" step fails for me.
 
virtualenv -p /usr/bin/python2.7 venv && . venv/bin/activate && pip install -r .ci/basic_python_requirements
/bin/sh: virtualenv: not found
 
Thanks in advance.
 
 
Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
Christian Gollwitzer <auriocus@gmx.de>: Feb 05 07:36PM +0100


> The "make venv" step fails for me.
 
> virtualenv -p /usr/bin/python2.7 venv && . venv/bin/activate && pip install -r .ci/basic_python_requirements
> /bin/sh: virtualenv: not found
 
"virtulenv" is a tool from the Python ecosystem. You can look if there
either is a BSD package for that (I don't use FreeBSD, so can't tell
what the command (emerge?) might be), or you can install it as a python
package using
 
pip install virtualenv
 
pip should come with python, or at least as a package on your system.
 
Christian
Christiano <christiano@engineer.com>: Feb 05 12:20AM

On Fri, 03 Feb 2017 15:29:23 -0800, Andrey Tarasevich wrote:
 
> consist of "immaterial" template definitions. In such cases
> "header-only" is not a goal in itself, it is a natural consequence (or
> even a necessity) rooted in the functionality of templates. The moment
a
> C++ library acquires a regular "material" definition, like a global
> variable or a non-inline function, it usually ceases to be "header-
only".
> everything into a header file. You'd ended up with a typical
> "header-only" library. No way around it.
 
> On the other hand, if some C++ library is 99.9% template/inline and
only
> would typically use non-header definitions), then one can say that
> erecting the traditional .h/.cpp/.a infrastructure on both the library
> side and client side just for some odd function might be an overkill.
In
> cases like that it might make more sense to keep things "header-only"
by
> declaring the function 'inline' (even if it is large).
 
> However, I'm also not surprised we see a tendency towards _non-
template_
> of effort translating it [almost] from scratch every time it is
> #include-d and then eliminating the resultant code duplication. Within
> certain reasonable limits this is a viable approach.
 
Okay, I understand, when someone does non-generic programs (not using
templates) then the traditional method of separating between .h and .cpp
happens naturally (even though there are projects using non-traditional
alternatives).
But when using templates, the template itself does not produce code (only
in the instance) and is then placed in the .h, in a natural way.
 
So, curiosity arises, does this imply that every generic library in c ++
is necessarily open source?
"Adam C. Emerson" <azure@fox.blue>: Feb 05 02:20AM

On 2017-02-05, Christiano <christiano@engineer.com> wrote:
[snip]
> way.
 
> So, curiosity arises, does this imply that every generic library in
> c ++ is necessarily open source?
 
No. See https://opensource.org/osd-annotated .
 
For software to qualify as 'open source', the author must not only
release the source code, but grant license for redistribution and
modification.
 
In Berne Convention countries, software is automatically covered by
copyright law, so unless there is a specific license grant, one is not
legally permitted to modify and redistribute it.
woodbrian77@gmail.com: Feb 04 06:56PM -0800

On Friday, February 3, 2017 at 5:23:14 PM UTC-6, Ian Collins wrote:
> > 37,217 -- in the .hh using __noinline
> > 37,392 -- in the .hh
 
> Does anyone, other than you, care?
 
Probably James Kanze. In the past, at least, he wasn't a
fan of header-only libraries.
 
And it's not like this is an isolated example. There's
another function in the same class that increases the size,
more than this function, if it's in the header.
 
 
Brian
Ebenezer Enterprises - "Respect thrift and economy, and
beware of debt." Philip Reed
 
http://webEbenezer.net
Ian Collins <ian-news@hotmail.com>: Feb 05 08:33PM +1300


>> Does anyone, other than you, care?
 
> Probably James Kanze. In the past, at least, he wasn't a
> fan of header-only libraries.
 
Most header only libraries are that way because they have to be.
 
> And it's not like this is an isolated example. There's
> another function in the same class that increases the size,
> more than this function, if it's in the header.
 
Probably because it gets inlined more often. The code may well run
faster because of this.
 
--
Ian
woodbrian77@gmail.com: Feb 05 09:41AM -0800

On Sunday, February 5, 2017 at 1:33:58 AM UTC-6, Ian Collins wrote:
> > more than this function, if it's in the header.
 
> Probably because it gets inlined more often. The code may well run
> faster because of this.
 
It might run faster on a machine that has spare memory.
Programs that don't hog resources though make for better
neighbors.
 
 
Brian
Ebenezer Enterprises - "Our world is a college, events are teachers,
happiness is the graduating point, character is the diploma G-d gives
man." Newell Dwight Hillis
 
http://webEbenezer.net
bitrex <bitrex@de.lete.earthlink.net>: Feb 05 10:56AM -0500

I've read about the "CRTP" and how it's used as a way to implement
polymorphic structures instead of using inheritance and virtual
functions, etc.
 
I'm wondering what its time/space overhead savings is over vtable based
inheritance is? Understanding of course the the downside is that all the
"overloads" are resolved at compile-time, not runtime.
 
Is it zero overhead? How exactly does the compiler deal with a class
which inherits from another class which is templated on its own subclass?
 
It's of interest to me because it seems like a more efficient way to
"inject" common behavior into subclasses in the "Policy-based design"
paradigm:
 
https://en.wikipedia.org/wiki/Policy-based_design
 
without the use of traditional dynamic inheritance.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 05 05:30PM +0100

On 05.02.2017 16:56, bitrex wrote:
> paradigm:
 
> https://en.wikipedia.org/wiki/Policy-based_design
 
> without the use of traditional dynamic inheritance.
 
Just measure.
 
It's a bit up and down but overall CRTP (static polymorphism) wins on
size and loses on clarity and functionality.
 
To understand how the compiler deals with things, and hence how you need
to code things, view a class declaration like
 
struct Derived;
 
struct Blah: Some_base
{
void foo() { say( "h3110!" ); }
};
 
struct Derived: Blah { using X = int; };
 
as shorthand for
 
struct Derived;
 
struct Blah
{
void foo();
};
 
struct Derived: Blah { using X = int; };
 
inline void Blah::foo() { say( "h3110!" ); }
 
Here in the definition of Blah you can't use anything from Derived,
because it's an incomplete class at this point. It doesn't have a size,
the compiler can't look into it to find e.g. a type X you'd like to use.
In particular you can't use X in the declaration of foo().
 
But, within the /definition/ of foo, class Derived is complete, and can
be used, including that you can use type X.
 
That's sort of obvious after applying the transformation of separating
out the member function definitions, but it can be bewildering before
one realizes that that's how the compiler deals with things here.
 
Cheers & hth.,
 
- Alf
ram@zedat.fu-berlin.de (Stefan Ram): Feb 05 02:26PM

>I had an interview at the central of London. And after i finished the
>avarage() function, he asked "thats all?"
 
In programming, details matter. So what one would need to know would
be the wording of what you were supposed to do. If it was, »Write a
function named "average"!«, a simple »void average(){}« would do.
 
>then he said: "..and maybe to throw an exception ?"
 
That would be a good point to start a discussion of wide versus
narrow contracts!
 
When should an average function have wide contract, when
does it should have a narrow contract?
 
I think C programmers likes narrow contracts while Java
programmers like wide contracts.
 
My personal preference is a narrow contract, because wide
contracts can be added as a wrapper. But when you only got
a wide contract you can never remove it to get a narrow one.
 
main.cpp
 
#include <algorithm>
#include <array>
#include <cstddef>
#include <iostream>
#include <ostream>
#include <stdexcept>
 
template< typename I >
inline static double narrow_average( I first, I top, size_t size )
{ return ::std::accumulate( first, top, 0.0 )/ size; }
 
template< typename I >
inline static double wide_average( I first, I top, size_t size )
{ if( size )return ::narrow_average( first, top, size );
else throw ::std::length_error{ "size must be nonzero for average." }; }
 
template< typename T, ::std::size_t N >
inline static double wide_average( ::std::array< T, N >const & a )
{ return ::wide_average( begin( a ), end( a ), N ); }
 
int main()
{ ::std::array< double, 4 >a{ { 1.0, 2.0, 3.0, 4.0 } };
::std::cout << ::wide_average( a )<< '\n';
return 0; }
 
transcript
 
2.5
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 05 06:55AM +0100

On 04.02.2017 23:29, Richard wrote:
>> I don't like that [...]
>> I don't like that [...]
 
> You should be opening issues on their github
 
That's a noble thought, to help them do the Right Thing™.
 
But I have a long history of helping Microsoft, including numerous MSVC
(and other product) bug reports, an unending stream of such, and that I
was a Microsoft "Most Valued Professional" in Visual C++ in 2012.
 
And my experience is that Microsoft folks /generally/ prefer denial
mode, to wit, Windows Explorer's design level bug where it scrolls away
a list you're trying to double click in (oh It's By Design™, yeah
right), but that their compiler team generally are more reality-oriented
and often fix bugs I report, surprisingly fast. Except in one special
case where they also went into denial mode. Then one old-timer there who
did the original code design took charge and forced them. :)
 
So, before engaging in trying to help Microsoft, one should have a fair
understanding of what that entails, negotiating a steep wall of denial.
 
That can take up much of one's time, as well as some emotional attrition
cost.
 
 
> instead of complaining here, a place they are unlikely to be
> reading.
 
Now that's silly: I was communicating to clc++ folks. Like you.
 
I see no reason yet to stop doing that. ;-)
 
 
Cheers!,
 
- Alf
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: