Page 1 of 1

Is this weird ?

Posted: November 2nd, 2017, 2:33 am
by MrGodin
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

Re: Is this weird ?

Posted: November 2nd, 2017, 4:30 am
by chili
It's a little weird, yeah. I don't see it as particularly harmful though.

Re: Is this weird ?

Posted: November 2nd, 2017, 8:07 am
by albinopapa
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

Re: Is this weird ?

Posted: November 2nd, 2017, 8:14 am
by albinopapa
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.

Re: Is this weird ?

Posted: November 3rd, 2017, 1:31 am
by MrGodin
@albinopapa .. Interesting stuff, I haven't done variable templates like that before