Is this weird ?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Is this weird ?

Post by MrGodin » November 2nd, 2017, 2:33 am

I found myself typecasting screenWidth / screenHeight variables so i'd thought i'd do this

Code: Select all

template<typename T>
	static T ScreenHeight()
	{
		return (T)m_screenHeight;
	};
template<typename T>
	static T ScreenWidth()
	{
		return (T)m_screenWidth;
	}; 
i guess going float height = Locator::ScreenHeight<float>() apposed to (float)Locator::ScreenHeight()
isn't all that different. Think i'll use it though
Curiosity killed the cat, satisfaction brought him back

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Is this weird ?

Post by chili » November 2nd, 2017, 4:30 am

It's a little weird, yeah. I don't see it as particularly harmful though.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Is this weird ?

Post by albinopapa » November 2nd, 2017, 8:07 am

Have you looked into variable templates?

template<typename T> T screenHeight;

Let's say your screen res doesn't change.
template<typename T>constexpr T screenHeight = static_cast<T>( 800 );
template<typename T>constexpr T screenWidth = static_cast<T>( 600 );

Then you can use them like so:
cout << screenHeight<float> << endl; // Prints screenHeight as a float
cout << screenHeight<int> << endl; // Prints screenHeight as an int
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Is this weird ?

Post by albinopapa » November 2nd, 2017, 8:14 am

As class members though, I think they need to be static, but you seem to be doing that anyway. If they do change however, then this wouldn't be a good fit. Changing the float version seems to create a new instance and doesn't change the int version for example.

Code: Select all

	{
		screenHeight<float> = 1024.f;
		cout << screenHeight<float> << endl;   // Prints 1024
		cout << screenHeight<int> << endl;      // Prints 800
	}

	{
		cout << screenHeight<float> << endl;    // Prints 1024
		cout << screenHeight<int> << endl;       // Prints 800
	}
So it's not a scoped change either.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Is this weird ?

Post by MrGodin » November 3rd, 2017, 1:31 am

@albinopapa .. Interesting stuff, I haven't done variable templates like that before
Curiosity killed the cat, satisfaction brought him back

Post Reply