Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to previous messageGo to previous message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
mirek wrote on Mon, 09 September 2019 18:59
That 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

 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Nested template question
Next Topic: FP exception vs NaN
Goto Forum:
  


Current Time: Fri Apr 26 11:53:49 CEST 2024

Total time taken to generate the page: 3.92026 seconds