Monday, November 3, 2014

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

comp.lang.c++@googlegroups.com Google Groups
Unsure why you received this message? You previously subscribed to digests from this group, but we haven't been sending them for a while. We fixed that, but if you don't want to get these messages, send an email to comp.lang.c+++unsubscribe@googlegroups.com.
ram@zedat.fu-berlin.de (Stefan Ram): Nov 03 11:27PM

>The way I see it, overloading is one facet of polymorphism, while
>inheritance is the other. Depends which interviewer you talk to, some
>have one in mind or the other or both. Best to go into both usually.
 
The term »polymorphism« was coined by Strachey in 1967. At
least this is the common wisdom, even though, AFAIK, Strachey
did not give proper definition at all, but just talked about
polymorphism. But maybe I just have not found his definition yet.
 
As far as I have access to Strachey's words, I cannot see
any reason why overloaded operators should not be called
»polymorphic«. On the opposite, Strachey said:
 
»In ad hoc polymorphism there is« ... »All the ordinary
arithmetic operations and functions come into this category.«!
 
Of course, he did not refer to C++ (in 1967), but possibly
to some programming language.
 
My personal definition of polymorphism is: I call a
function symbol »f« polymorphic, when the meaning of
»f(x)« is mapped to one of »f1(x)«, »f2(x)«, ... by
the type of x. This definition encompasses operator
overloading. In C++, operator overloading is compile-
time polymorphism, not run-time polymorphism, though.
 
(By my definition »polymorphism« is an implementation
detail. Therefore, it is possible that, sometimes, one does
not know whether a function symbols is polymorphic. For
example, I might know that »-« can be applied to both int
and double. When the processor used has a single CHS opcode
for both int and double values, there is only one
implementation, so it is not polymorphic. »-« always means
»CHS«. Another processor used might have a »CHSI« for int
and a »CHSD« for double, and C++ maps »-« to »CHSI« or
»CHSD« depending of the type of the operand for this other
processor, so in this case »-« is polymorphic. But for the
C++ programmer it suffices to know that he can apply it to
both int and double with the intended semantics.)
Ian Collins <ian-news@hotmail.com>: Nov 04 10:33AM +1300

Christopher Pisz wrote:
>>> #include <stdlib.h>
 
>> Deprecated by whom?
 
> by <cstdlib>
 
A header can only be deprecated by the standard, not by another header.
 
The general consensus on projects I've working on is the <cxxx> headers
are a wast of time: if you are using the C standard library functions,
don't try and hide them.
 
--
Ian Collins
Ben Bacarisse <ben.usenet@bsb.me.uk>: Nov 03 10:29PM


>> Please show me Ian, how do we determine the difference between a
>> valid value of zero and an error Ian.
 
> Check errno... and reset errno to 0 before call.
 
strtoul does not set errno when zero is returned, so you can't use it to
find out anything about a zero return. It can be used, however, to
detect out of range inputs.
 
<snip>
--
Ben.
Luca Risolia <luca.risolia@linux-projects.org>: Nov 03 05:19PM +0100

JiiPee wrote:
 
 
> #include <iostream>
> #include <ctime>
 
> inline void intToStr(unsigned val, char* c)
 
 
This should be ~4x faster, just for fun...
 
#include <cstdint>
#include <limits>
 
template <class T>
constexpr T pow(T const& x, std::size_t n) {
return n > 0 ? x * pow(x, n - 1) : 1;
}
 
constexpr auto dgts = std::numeric_limits<unsigned int>::digits10;
constexpr auto max = pow(10u, dgts);
 
template<int> struct S;
 
template<>
struct S<0> {
template<int>
static inline void f(unsigned int, char*) noexcept {}
};
 
template<int P>
struct S {
template<int Q>
static inline void f(unsigned int i, char* c) noexcept {
constexpr auto a = pow(10u, Q + 1 - P), b = a / 10;
S < P - 1 > ::template f<Q>(i, c);
c[P - 1] = '0' + (i % a) / b;
}
};
 
template<int> void g(unsigned int, char*) noexcept;
 
template<> inline void g<dgts + 1>(unsigned int i, char* c) noexcept {
if (i < max) {
S<dgts>::f<dgts>(i, c);
c[dgts] = '\0';
} else {
c[0] = '0' + (i / max);
S<dgts >::f < dgts > (i, c + 1);
c[dgts + 1] = '\0';
}
}
 
template<int X> inline void g(unsigned int i, char* c) noexcept {
constexpr unsigned int Y = pow(10u, X);
if (i < Y) {
S<X>::template f< X > (i, c);
c[X] = '\0';
} else
g < X + 1 > (i, c);
}
 
inline void uint_to_str(unsigned int i, char* c) noexcept {
g<1>(i, c);
}
 
int main() {
// example
char str[16];
uint_to_str(100, str);
}
Martijn Lievaart <m@rtij.nl.invlalid>: Nov 03 05:47PM +0100

On Mon, 03 Nov 2014 17:19:07 +0100, Luca Risolia wrote:
 
> } else
> g < X + 1 > (i, c);
> }
 
What if i is slightly smaller than std::numeric_limits<unsigned>::max()?
In that case Y would eventually not fit an unsigned, resulting in an
infinite loop.
 
M4
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: