Monday, April 12, 2021

Digest for comp.lang.c++@googlegroups.com - 3 updates in 2 topics

Juha Nieminen <nospam@thanks.invalid>: Apr 12 05:50AM

> There's always strtol() and friends.
 
This sub-discussion spawned from someone suggesting that since the
original question was about scanf() that he go to the C group, and
me asking why, given that scanf() is a 100% C++ standard function,
and then someone responding with using the "C++ equivalent" of
scanf(), ie. std::istream (&co) as an alternative to the originally-C
function.
 
But yes, there's absolutely nothing wrong in using the standard library
functions that were "inherited" from C, if they suit the task at hand.
 
(On that note, one could argue that if scanf() is being used because of
its somewhat-syntax-parsing capability, kind of, which none of those
other suggestions support, it would be better to use a combination of
std::regex and those string-to-integer functions, especially if absolute
maximum efficiency is not required, as std::regex allows for a *significalty*
higher degree of syntax parsing than scanf(). Not that it completely
replaces a full-fledged parser, but much more so than scanf().)
Ian Collins <ian-news@hotmail.com>: Apr 12 08:13PM +1200

On 12/04/2021 17:50, Juha Nieminen wrote:
> maximum efficiency is not required, as std::regex allows for a *significalty*
> higher degree of syntax parsing than scanf(). Not that it completely
> replaces a full-fledged parser, but much more so than scanf().)
 
Rather than going down that route, with my variadic function templates I
just beak down the format string and mix it with the variables to be
loaded, for example:
 
 
unsigned a {}, b {}, c {};
const auto read = fromString( "V1.2.3", "V", c, ".", b, ".", c );
 
I find it easier to read when not having to mentally map format
specification to the variables.
 
--
Ian,
Paavo Helde <myfirstname@osa.pri.ee>: Apr 12 09:25AM +0300

11.04.2021 23:05 Richard Harnden kirjutas:
>>   }
 
>>   My code lose of readability but i don't know how to get around this
>>   problem.
 
The simplest solution is to move #ifdef inside the helper functions and
trust the compiler to optimize away the calls if empty:
 
void DebugPush(auto& iters, auto iteration) {
#ifdef TEST_ALGORITM
iters.iterations.push_back(iteration);

No comments: