Thursday, January 12, 2017

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

ram@zedat.fu-berlin.de (Stefan Ram): Jan 12 09:52PM

>, where array is arr's array. But it has this extra temporary variable
>int index{1}; . So how can I get rid of that temporary?
 
Don't worry. The optimizer can deal with such minutiae.
 
I'd write this as
 
int & get( int const index ) const { return array[ i ]; }
 
(untested).
 
Guidelines:
 
SL.10: Prefer using STL array or vector instead of a C array
 
Bounds.2: Only index into arrays using constant expressions.
ram@zedat.fu-berlin.de (Stefan Ram): Jan 12 10:15PM

>int & get( int const index ) const { return array[ index ]; }
...
>Bounds.2: Only index into arrays using constant expressions.
 
And, no, I do /not/ deem »index« to be a constant expression
above, so I did /not/ write »const« because of this. Instead
I wrote »const« because of
 
Guideline:
 
Con.4: Use const to define objects with values that do not
change after construction
ram@zedat.fu-berlin.de (Stefan Ram): Jan 12 10:21PM

>Con.4: Use const to define objects with values that do not
>change after construction
 
If you prefer Scott Meyers:
 
»Use const whenever possible.« -- SCOTT MEYERS
ram@zedat.fu-berlin.de (Stefan Ram): Jan 12 10:30PM

>>change after construction
>If you prefer Scott Meyers:
>»Use const whenever possible.« -- SCOTT MEYERS
 
Sorry to followup on this one more time!
 
I'd like to put it this way:
 
"Use const whenever possible.''
 
SCOTT MEYERS
 
"Guideline: Con.4: Use const to define objects with
values that do not change after construction"
 
C++ Core Guidelines; April 7, 2016; BJARNE STROUSTRUP and
HERB SUTTER
 
Who are Scott Meyers, Bjarne Stroustrup and Herb Sutter?
These are the three world's leading "C++ evangelists", so
as to say.
 
(I do not say "C++ experts", because otherwise people like
Andrei Alexandrescu would have to be included.)
 
So all three agree in this point.
 
I always practiced this, since the 80s, when I used
»const« in Pascal.
ram@zedat.fu-berlin.de (Stefan Ram): Jan 12 11:23PM

>>>int index{1}; . So how can I get rid of that temporary?
>>Don't worry. The optimizer can deal with such minutiae.
>Are you 100% sure?
 
No, optimizers are implementation specific.
 
>it did not deal with it when I used an object (instead of and
>index)
 
How can you know whether this really runs slower?
 
You need to compare it with a microbenchmark.
 
First, learn how to do profiling and microbenchmarks.
 
Then compare the two possibilities.
 
You will get an answer, but only for a specific C++
implementation and execution environment.
JiiPee <no@notvalid.com>: Jan 12 11:15PM

On 12/01/2017 22:30, Stefan Ram wrote:
 
> SCOTT MEYERS
 
> "Guideline: Con.4: Use const to define objects with
> values that do not change after construction"
 
sure
JiiPee <no@notvalid.com>: Jan 12 11:15PM

On 12/01/2017 21:52, Stefan Ram wrote:
>> , where array is arr's array. But it has this extra temporary variable
>> int index{1}; . So how can I get rid of that temporary?
> Don't worry. The optimizer can deal with such minutiae.
 
Are you 100% sure? it did not deal with it when I used an object
(instead of and index)
"Chris M. Thomasson" <invalid@invalid.invalid>: Jan 12 03:03PM -0800

On 1/11/2017 2:50 PM, Chris M. Thomasson wrote:
> ct_float escape;
> ct_float epsilon;
 
> unsigned int imax;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
> };
 
[...]
 
> 2,
> .0001,
> 500000000
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
> };
 
Ummm... unsigned int can only be assumed to hold a 16-bit value. AFAICT,
500000000 is out-of-range wrt portability and correctness.
 
I should have used long here. Sorry.
Vir Campestris <vir.campestris@invalid.invalid>: Jan 12 09:40PM

On 10/01/2017 21:48, David Brown wrote:
>> not so in English, where it's related to jolt.
 
> "jerk" is the same thing as "jolt" - it is the derivative of
> acceleration with time.
 
Yes, but you learned English, not American...
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: