Tuesday, July 17, 2018

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

Ian Collins <ian-news@hotmail.com>: Jul 10 08:46PM +1200

On 10/07/18 20:41, Alf P. Steinbach wrote:
> auto mod( Number a, Number b )
> -> Number
> { return a%b; }
 
Given your love of suffix return, I'm surprised you don't use C++ >= 14
rules and simply write:
 
template<class Number, class = enable_if_t<is_integral_v<Number>>>
auto mod( Number a, Number b ) { return a%b; }
 
--
Ian.
Soviet_Mario <SovietMario@CCCP.MIR>: Jul 09 08:22PM +0200

Il 09/07/2018 20:14, Soviet_Mario ha scritto:
 
> ...
> ...
 
> }
 
 
sorry I mispressed RETURN before completing the post :\
 
I also wanted to ask a question : whether or not (and how)
in "plain" C++ would be possible to specify (always in a
template) a typename WITH RESTRICTIONS (as in VStudio
extension).
 
With restriction I mean, a typename parameter must be within
a subset of types, specified in some form (ex. in brackets).
 
I'm not intersted to obtain INTERFACE filter, just to filter
types.
 
That is for the fact that non necessarily a template logic
is consistent with every type ! I mean, a template built fo
manage number and quantities in calculation would be
meaningless passing "std::string" as a typename parameter
(as strings can hold illegal number values).
 
How to restrict the space of typenames to a subset, stating
legal types ?
 
Re-thanks
 
 
--
1) Resistere, resistere, resistere.
2) Se tutti pagano le tasse, le tasse le pagano tutti
Soviet_Mario - (aka Gatto_Vizzato)
Soviet_Mario <SovietMario@CCCP.MIR>: Jul 10 01:26AM +0200

Il 09/07/2018 22:41, Scott Lurndal ha scritto:
 
>> std:array has no extra overhead.
 
> Personally, I'd rather see 'char zBuf[param];'. I see no benefit in
> another layer of abstraction over a native type.
 
I agree, in case of a STATIC array who does not offer
dynamic resizing and other extras ...
 
 
--
1) Resistere, resistere, resistere.
2) Se tutti pagano le tasse, le tasse le pagano tutti
Soviet_Mario - (aka Gatto_Vizzato)
Soviet_Mario <SovietMario@CCCP.MIR>: Jul 09 09:17PM +0200

Il 09/07/2018 21:04, Alf P. Steinbach ha scritto:
 
>     template< class Number, class = enable_if_t<
> is_integral_v<Number> >
>     auto mod( Number a, Number b ) -> Number { return a%b; }
 
I'm going to document myself about this feature, thanks for
signalling me !
 
>> within a subset of types, specified in some form (ex. in
>> brackets).
 
> You might be able to leverage std::tuple for that.
 
oh ... I used to think this feature was only supported by
BOOST (who was very complex to compile and slow). I'm very
glad that now tuple become part of standard !
 
 
> Otherwise, it can be a good exercise to make  your own type
> list.
 
No tnx, I'm just grasping trying to use more rock solid
components written by more skilled progrtammers and debugged
well. I've lost all my juvenile hubris in trying these things :)
TY
 
there's only one case when I try to code myself : when I
really can't understand the logic of the existing components
and doubt about using them as intended. In that case, I
prefer to suspect where errors may hide to suspecting period.
 
 
--
1) Resistere, resistere, resistere.
2) Se tutti pagano le tasse, le tasse le pagano tutti
Soviet_Mario - (aka Gatto_Vizzato)
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 10 10:41AM +0200

On 10.07.2018 03:05, Soviet_Mario wrote:
> consecutive closed (with a space not to be parsed as >> op.   True ?
> Sorry for the futile remark, but ... well, I'm not that sure where it
> should go.
 
Yes, just place it at the end. The spaces are not really necessary in
C++11 and later.
 
--------------------------------------------------------------------
#include <type_traits> // std::is_integral_v
#include <utility> // std::enable_if_t
using namespace std;
 
template<class Number, class = enable_if_t<is_integral_v<Number>>>
auto mod( Number a, Number b )
-> Number
{ return a%b; }
 
#include <iostream>
int main()
{
const int a = mod( 13, 5 );
#ifdef TEST
const double b = mod( 13.0, 5.0 ); //! Won't compile.

No comments: