Friday, July 24, 2015

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

ram@zedat.fu-berlin.de (Stefan Ram): Jul 19 07:02PM

>And what's C++ all about, if it's not about being pedantic? :)
 
»I have never, ever, ever seen a great software developer
who does not have amazing attention to detail.«
 
http://www.softwarebyrob.com/articles/Personality_Traits_of_the_Best_Software_Developers.aspx
 
Programming is about expressing your ideas so that other
people can understand them,
 
"Programming is understanding." - Kristen Nygaard
 
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand."
- Martin Fowler
 
This suggest that it is about using a language correctly.
 
»I've found that some of the best [Software ]developers
of all are English majors. They'll often graduate with
no programming experience at all, and certainly without
a clue about the difference between DRAM and EPROM.
 
But they can write. That's the art of conveying
information concisely and clearly. Software development
and writing are both the art of knowing what you're going
to do, and then lucidly expressing your ideas.«
 
Paul Potts
http://praisecurseandrecurse.blogspot.com/2007/03/english-majors-as-programmers.html
 
»Besides a mathematical inclination, an exceptionally
good mastery of one's native tongue is the most vital
asset of a competent programmer.«
 
Edsgar Dijkstra
 
»While sloppy writing does not invariably mean sloppy
thinking, we've generally found the correlation to be
strong -- and we have no use for sloppy thinkers.
If you can't yet write competently, learn to.«
 
Eric Raymond
 
http://www.catb.org/~esr/faqs/hacker-howto.html#skills4
 
»The narrative measures of conjunction use, event
content, perspective shift, and mental state reference
were significantly predictive of later Math scores.«
 
http://www.arts.uwaterloo.ca/%7Edoneill/papers/Storytelling%20and%20math.pdf
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jul 17 06:51PM

On Fri, 2015-07-17, Doug Mika wrote:
> class of vector<int> class? Is it?
 
> Does anyone know of a easy to read and comprehensive introduction to
> how/when/why to use the scope resolution operator?
 
*Shrug* Call me lazy, but I use :: when I have to; when the compiler
refuses to understand me if I don't.
 
For vector<int>::const_iterator it's obvious that I have to; there is
no all-purpose "const_iterator", and I'm not inside the implementation
of vector<int>, so I have to be specific (or use "auto").
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 02:16PM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> So what precisely is the line inside vector<T>
 
> typedef ? const_iterator;
 
http://www.learncpp.com/cpp-tutorial/46-typedefs/
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Jul 19 04:49AM -0600

On Fri, 17 Jul 2015 09:52:11 -0700 (PDT), Doug Mika
<dougmmika@gmail.com> wrote:
 
.. snip ..
>So it begs the question, when we write vector<int>::const_iterator myIterator
>can I deduce from this that const_iterator is an inner class of vector<int> class? Is it?
 
To be pedantic, it doesn't *beg* the question, it *raises* the
question. Hopefully, this link is accessible without a New York Times
subscription:
 
http://afterdeadline.blogs.nytimes.com/2008/09/25/begging-the-question-again/
 
That links to this page, which might be helpful:
 
http://begthequestion.info/
 
And what's C++ all about, if it's not about being pedantic? :)
 
Louis
legalize+jeeves@mail.xmission.com (Richard): Jul 20 06:59PM

[Please do not mail me a copy of your followup]
 
Louis Krupp <lkrupp@nospam.pssw.com.invalid> spake the secret code
 
>And what's C++ all about, if it's not about being pedantic? :)
 
s/C++/comp.lang.c++/ and I'm in agreement.
 
A tautology is a thing that is tautological.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
doctor@doctor.nl2k.ab.ca (The Doctor): Jul 20 03:02PM

Got a weird, maybe some gurus might be able to help me out.
 
By mistake I tpe gmke -j .
 
Gmake being gnu make.
 
since then on one system, the C compiler has not beeing working properly.
 
Test errors, programmes not seeing the correct data,
socket not starting ...
 
 
What can I do to rectify this situation?
--
Member - Liberal International This is doctor@@nl2k.ab.ca Ici doctor@@nl2k.ab.ca
God,Queen and country!Never Satan President Republic!Beware AntiChrist rising!
http://www.fullyfollow.me/rootnl2k Look at Psalms 14 and 53 on Atheism
Abuse a man unjustly, and you will make friends for him. -Edgar Watson Howe
James Kuyper <jameskuyper@verizon.net>: Jul 20 01:39PM -0400

On 07/20/2015 11:02 AM, The Doctor wrote:
 
> Test errors, programmes not seeing the correct data,
> socket not starting ...
 
> What can I do to rectify this situation?
 
The -j option should only directly affect the files created by the run
of gmake where you used that option. The effect might propagate to other
things that were built using one of those files. If you erase all such
files, and start over again, any problem actually related to your use of
the -j option should disappear.
 
I strongly suspect that many, if not all, of the symptoms you describe
had nothing to do with your use of the -j option - there's no obvious
reason why they should. If I'm right about that, you can expect those
symptoms to continue occurring until you track down and correct the
actual cause of those problems.
Keith Thompson <kst-u@mib.org>: Jul 20 11:23AM -0700

> Got a weird, maybe some gurus might be able to help me out.
 
> By mistake I tpe gmke -j .
 
> Gmake being gnu make.
 
I presume you mean that you typed "gmake -j". I don't usually complain
about typos, but these make your question difficult to read.
 
 
> Test errors, programmes not seeing the correct data,
> socket not starting ...
 
> What can I do to rectify this situation?
 
The "make" command will execute whatever commands are specified in the
default Makefile. Those commands will have whatever effect they would
have if you typed them at a shell prompt. Without knowing what's in
your Makefile, it's impossible to know what your "gmake -j" command has
done.
 
There's a convention for Makefiles to have a "clean" target which will
remove any generated files. It's possible that typing "gmake clean"
will undo whatever damage you've done. I accept no responsibility for
any consequences, which again are entirely dependent on the contents of
the Makefile.
 
(The default makefile may or may not be called "Makefile". Consult the
documentation for details.)
 
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 01:56AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> Why is the following a compile time error and not a bad_cast
> exception:
 
Because dynamic_cast is defined to work only on pointers and references.
 
> ampersands (&) in
 
> Derived& rd = dynamic_cast<Derived&>(b);
 
> but why?
 
Because dynamic_cast is defined to work only on pointers and references.
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 11:09AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> So in the example that compiles, with ampersands, (I include the
> example again below) why doesn't it simply return rd=nullptr, why does
> it throw an exception.
 
A reference cannot have nullptr value.
 
> My question I guess is, when does dynamic cast
> throw an exception and when does it return nullptr?
 
It returns nullptr if this is possible, otherwise throws.
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 01:50AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> Why doesn't the following program throw a bad_cast exception? How can
> the cast possibly suceed?
 
It does not succeed - the resulting pointer will be NULL. The bad_cast
exception is thrown only when casting to a reference type. By using
either a pointer or a reference type you decide whether you want an
automatic exception or not.
 
Paavo Helde <myfirstname@osa.pri.ee>: Jul 17 11:06AM -0500

Doug Mika <dougmmika@gmail.com> wrote in
 
> What do you mean by an automatic exception?
 
bad_cast
Barry Schwarz <schwarzb@dqel.com>: Jul 14 05:23PM -0700

On Tue, 14 Jul 2015 16:53:30 -0700 (PDT), fl <rxjwg98@gmail.com>
wrote:
 
 
>Experience in C/C++ code optimization for speed improvement.
 
>I can only get the following answers:
 
>1. Use reference to replace a direct parameter in function call;
 
Benefit is realized only when argument is a very large aggregate or
function is called very frequently.
 
>2. Use register for some frequent variables;
 
Compiler is free to ignore suggested storage class.
 
>3. Use assembly code to replace C code
 
No longer C or C++ and a maintenance nightmare.
 
>4. Unroll a loop(?)
 
Is this practical if the iteration count is not constant.
 
>What is your opinion to the question?
 
The best way of improving speed is to select or design a speed
efficient algorithm. For a trivial example, computing factorials will
be faster in a loop than using recursion.
 
--
Remove del for email
Victor Bazarov <v.bazarov@comcast.invalid>: Jul 14 08:29PM -0400

On 7/14/2015 7:53 PM, fl wrote:
> I am given the following question:
 
> Experience in C/C++ code optimization for speed improvement.
 
That's not a question. AFAIK, a question starts with an auxiliary verb
or with a special word like "where" or "who" or "what" or "how", and
ends with a question mark ("?"). Here you have a directive or an
incomplete sentence.
 
> 3. Use assembly code to replace C code
> 4. Unroll a loop(?)
 
> What is your opinion to the question?
 
My opinion is that the questioner doesn't know the subject.
 
0. There is no such thing as C/C++.
 
As for the choices for optimizing the speed, 1 and 4 are viable. Using
'register' is but a hint to the compiler. Choice 3 seems nothing to do
with C++ (the topic of this newsgroup).
 
V
--
I do not respond to top-posted replies, please don't ask
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 15 05:07AM +0200

On 15-Jul-15 1:53 AM, fl wrote:
 
> I am given the following question:
 
> Experience in C/C++ code optimization for speed improvement.
 
I read that as, "list your experience with speed optimization for C and
C++ code".
 
 
> 3. Use assembly code to replace C code
> 4. Unroll a loop(?)
 
> What is your opinion to the question?
 
First, it's about particular programming languages, so presumably if you
have experience with MEASURING, which is the most important thing, then
you are expected to relate something of how you did that differently for
the two mentioned languages, than for some other possible languages. I
think you would be expected to mention some common tools and approaches
for this, such as profiler, Unix-land `time` command, instrumenting code
with timers and logging, and just dead reckoning (which can tell you if
other ways are necessary). Regarding timers it would be nice if you know
about the very limited resolution of C library time in Windows, and
knowing about higher resolution C++11 timers, and about Boost timers.
 
Second, after measuring comes algorithmic considerations. Replacing an
O(n^2) algorithm with an O(n) algorithm is common. E.g. I think it would
count in a positive way if you mentioned how you once optimized some
repeated string concatenation, perhaps in Java. Knowing about support
for this in C++11 would be extra point.
 
Third, simple mechanical optimizations like you mention above. But your
point (2), "Use register for some frequent variables", would count
NEGATIVELY if I were to evaluate the answer, unless this is about an
application to become part of a compiler development team (for ordinary
app code you should leave that to the compiler: forcing it you're likely
to do worse). On the other hand, knowing that the "register" keyword was
deprecated in C++11 would be positive.
 
 
Cheers & hth.,
 
- Alf
 
--
Using Thunderbird as Usenet client, Eternal September as NNTP server.
"Lőrinczy Zsigmond" <nospam@for.me>: Jul 15 06:38AM +0200

Forget it. 'Smart tricks' won't give you more than a few percent.
Plus kindly remember:
Premature optimization is the root of all evil
Paavo Helde <myfirstname@osa.pri.ee>: Jul 15 12:22AM -0500

fl <rxjwg98@gmail.com> wrote in news:aaa4556e-8239-412c-be9d-7308a1a2cbb6
 
> I am given the following question:
 
> Experience in C/C++ code optimization for speed improvement.
 
This is not a question. If this is a requirement for a new hire, then I
would think this means the ability to run a profiler like
valgrind+callgrind, ability to interpret results and ability to modify the
code to get rid of bottlenecks found this way.
 
hth
Paavo
bartekltg <bartekltg@gmail.com>: Jul 15 03:10PM +0200

On 15.07.2015 02:23, Barry Schwarz wrote:
 
>> 1. Use reference to replace a direct parameter in function call;
 
> Benefit is realized only when argument is a very large aggregate or
> function is called very frequently.
 
 
T& and const T& is quite good habit. In most cases it will
be used with containers. And if we use it for small types
(for example because we write const T & in template, and then
use if with 'int') compiler regardless will copy value.
 
>> 2. Use register for some frequent variables;
 
> Compiler is free to ignore suggested storage class.
 
Even worse.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
D.2 register keyword [depr.register]
1 The use of the register keyword as a storage-class-specifier (7.1.1)
is deprecated.
 
 
 
 
> The best way of improving speed is to select or design a speed
> efficient algorithm. For a trivial example, computing factorials will
> be faster in a loop than using recursion.
 
IMHO it shoud be threated as the same algorithm, just different
execution. I think changing recursion to loops, aside from
oblivious cases like factorial, is premature optimalization.
And in simple cases it change nothing, compiler will change
it for us:
 
c++ code:
int factorial (int x){
if (x>1) return x*factorial(x-1);
else return x;
}
 
asm generated by gcc
 
.type _Z9factoriali, @function
_Z9factoriali:
.LFB1879:
.cfi_startproc
cmpl $1, %edi
jle .L50
movl $1, %edx
.p2align 4,,10
.p2align 3
.L49:
leal -1(%rdi), %eax
imull %edi, %edx
cmpl $1, %eax
movl %eax, %edi
jne .L49
.L48:
imull %edx, %eax
ret
.L50:
movl %edi, %eax
movl $1, %edx
jmp .L48
 
 
Just a simple loop;-)
 
bartekltg
JiiPee <no@notvalid.com>: Jul 16 05:00AM +0100

On 15/07/2015 14:10, bartekltg wrote:
> if (x>1) return x*factorial(x-1);
> else return x;
> }
 
factorial maybe not the best example as I would do it using constexpr:
constexpr long long fac(int k)
{
return (k == 0) ? 1 : k * fac(k - 1);
}
giving an instant answer (calculated compile time).
bartekltg <bartekltg@gmail.com>: Jul 16 01:23PM +0200

On 16.07.2015 06:00, JiiPee wrote:
> return (k == 0) ? 1 : k * fac(k - 1);
> }
> giving an instant answer (calculated compile time).
 
Sure...
 
main(){
big_int x;
cin>>c;
cout<<fac(x);
}
 
;-)
 
Also, instead int we have for example arthmetic modulo 1 178 973 853,
so x can be too bit to put every possible answer in a array.
 
bartekltg
JiiPee <no@notvalid.com>: Jul 16 04:48PM +0100

On 16/07/2015 09:04, Öö Tiib wrote:
 
> Even IEEE double precision can't contain 23! accurately and will
> overflow at 171! so usefulness of factorial is rather narrowly
> limited.
 
I did not mean it to be perfectly done (just the idea), but you can
easily change it to something like:
 
constexpr long long unsafeFac(int k)
{
return (k == 0) ? 1 : k * fac(k - 1);
}
 
constexpr long long fac(int k)
{
return (k < 0 || k > 15) ?|||throw| |exception()| : unsafeFac(k);
}
 
Now it complains *compile* time the error (which is definitely better than a runtime version) if k is too big or too small.
 
 
 
 
JiiPee <no@notvalid.com>: Jul 16 04:52PM +0100

On 16/07/2015 16:48, JiiPee wrote:
> {
> return (k < 0 || k > 15) ?|||throw| |exception()| : unsafeFac(k);
> }
 
ups, letters messed up, should be:
constexpr long long fac(int k)
{
return (k < 0 || k > 15) ? throw exception() : unsafeFac(k);
}
 
 
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jul 16 05:58PM

On Tue, 2015-07-07, Alf P. Steinbach wrote:
 
> In C++ a reference refers to the result of the evaluation of the
> initializer expression. But (as I recall) due to a sloppy formulation of
> the specification of an early version of the Algol language, [...]
 
Etc. In C++ -- (and all real languages. except pure pre-processor
languages like cpp[0]) -- expressions are evaluated where they are, in
their current context. So "index" is 5, and "ref" is a reference to
vec[5], whatever that is at that point in time. Like any other
reference into a std::vector, it's vulnerable to invalidation, though.
For example, if you add enough elements to "vec" to force it to
reallocate itself, "ref" will point to something crazy and
unpredictable. If you don't it's just a sane reference to vec[5].
 
/Jorgen
 
[0] cpp and make are the only remaining languages with this property
that I can think of. Maybe m4 as well. Too bad that one of them
is embedded into every C and C++ compiler ...
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
legalize+jeeves@mail.xmission.com (Richard): Jul 16 04:57PM

[Please do not mail me a copy of your followup]
 
Hi,
 
I've contributed a check to clang-tidy that simplifies
statements involving boolean literals. The name of the check is
simplify-boolean-expr. It is not enabled by default, so you must request
this check to get fixes applied to your code by clang-tidy.
 
This has been checked into the trunk of clang and I believe will be
available only by compiling the clang-tools-extra subrepository of the
clang/llvm tree. I've been trying to get clang-tidy included in the
regular distribution, but it hasn't made it into all the packages yet
AFAIK. So you'll need to compile it from source to use it. This isn't
hard, but it is a fairly lengthy build.
 
I believe this check is sufficiently robust that it should make no changes
to your code that change the semantics of the code. I've run this on
the clang and LLVM code bases and addressed issues and made improvements
to correct and such problems encountered there. However, I would love
for this check to be run on more code bases to check for any additional
problems I might have missed. You can provide feedback directly to me
by email or via the clang bug tracker. <https://llvm.org/bugs/>
 
This check looks for boolean expressions involving boolean constants and
simplifies them to use the appropriate boolean expression directly.
 
Examples:
`if (b == true)` becomes `if (b)`
`if (b == false)` becomes `if (!b)`
`if (b && true)` becomes `if (b)`
`if (b && false)` becomes `if (false)`
`if (b || true)` becomes `if (true)`
`if (b || false)` becomes `if (b)`
`e ? true : false` becomes `e`
`e ? false : true` becomes `!e`
`if (true) t(); else f();` becomes `t();`
`if (false) t(); else f();` becomes `f();`
`if (e) return true; else return false;` becomes `return e;`
`if (e) return false; else return true;` becomes `return !e;`
`if (e) b = true; else b = false;` becomes `b = e;`
`if (e) b = false; else b = true;` becomes `b = !e;`
`if (e) return true; return false;` becomes `return e;`
`if (e) return false; return true;` becomes `return !e;`
 
The resulting expression `e` is modified as follows:
1. Unnecessary parentheses around the expression are removed.
2. Negated applications of `!` are eliminated.
3. Negated applications of comparison operators are changed to use the
opposite condition.
4. Implicit conversions of pointer to `bool` are replaced with explicit
comparisons to `nullptr`.
5. Implicit casts to `bool` are replaced with explicit casts to `bool`.
6. Object expressions with `explicit operator bool` conversion operators
are replaced with explicit casts to `bool`.
 
Examples:
1. The ternary assignment `bool b = (i < 0) ? true : false;` has redundant
parentheses and becomes `bool b = i < 0;`.
 
2. The conditional return `if (!b) return false; return true;` has an
implied double negation and becomes `return b;`.
 
3. The conditional return `if (i < 0) return false; return true;` becomes
`return i >= 0;`.
The conditional return `if (i != 0) return false; return true;` becomes
`return i == 0;`.
 
4. The conditional return `if (p) return true; return false;` has an
implicit conversion of a pointer to `bool` and becomes
`return p != nullptr;`.
The ternary assignment `bool b = (i & 1) ? true : false;` has an implicit
conversion of `i & 1` to `bool` and becomes
`bool b = static_cast<bool>(i & 1);`.
 
5. The conditional return `if (i & 1) return true; else return false;` has
an implicit conversion of an integer quantity `i & 1` to `bool` and becomes
`return static_cast<bool>(i & 1);`
 
6. Given `struct X { explicit operator bool(); };`, and an instance `x` of
`struct X`, the conditional return `if (x) return true; return false;`
becomes `return static_cast<bool>(x);`
 
When a conditional boolean return or assignment appears at the end of a
chain of `if`, `else if` statements, the conditional statement is left
unchanged unless the option `ChainedConditionalReturn` or
`ChainedConditionalAssignment`, respectively, is specified as non-zero.
The default value for both options is zero.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
legalize+jeeves@mail.xmission.com (Richard): Jul 15 04:57PM

[Please do not mail me a copy of your followup]
 
Ian Collins <ian-news@hotmail.com> spake the secret code
 
>That is one solution I have used in the past, however mocking them
>required a lot less work - especially if the mocking framework generates
>all of the code for you.
 
Yeah, it's a judgment call. If you're using a handfull of functions
from the Win32 API, then creating your own domain-specific wrapper
around them can be easy. If you need to wrap hundreds of API calls
from Win32 then you'll want something more automated.
 
On the flip side, sometimes you end up with better architecture as a
side benefit of creating the insulting domain specific layer. You end
up with interfaces that more directly describe the specific services
needed by your application.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
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: