Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Dumb bug. Improper use of Null
Re: Improper use of Null [message #52333 is a reply to message #52332] |
Mon, 09 September 2019 22:27   |
Sender Ghost
Messages: 301 Registered: November 2008
|
Senior Member |
|
|
mirek wrote on Mon, 09 September 2019 18:59That said, perhaps we could introduce something like
int64 x = NvlTo<int64>(s);
Something like this?
#include <Core/Core.h>
using namespace Upp;
template <class T, class C>
T NvlTo(const C& x)
{
if (IsNull(x))
return Null;
return x;
};
template <class T>
void Print(const T& x)
{
if (IsNull(x))
Cout() << "I wanted this\n";
else
Cout() << x << '\n';
}
CONSOLE_APP_MAIN
{
#if 1
int a = Null;
int64 b = NvlTo<int64>(a);
Print(b);
a = -10;
b = NvlTo<int64>(a);
Print(b);
#else
int a = Null;
int64 b, c = 0;
const int n = 1000000000;
{
RTIMING("NvlTo");
for (int i = 0; i < n; ++i) {
b = NvlTo<int64>(a);
c += b + 1;
}
}
Print(c);
ASSERT(c == n);
c = 0;
{
RTIMING("Value");
for (int i = 0; i < n; ++i) {
b = Value(a);
c += b + 1;
}
}
Print(c);
ASSERT(c == n);
#endif
}
With following results:
I wanted this
-10
Thanks.
[Updated on: Tue, 10 September 2019 21:10] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Wed Apr 30 06:36:41 CEST 2025
Total time taken to generate the page: 0.04036 seconds
|