Page 2 of 2

Re: How dangerous is this class exactly?

Posted: June 9th, 2016, 4:33 am
by albinopapa
Questions,

What do you pass in for the template parameters?
Why do you use const rvalue references when you are trying to use move semantics, and why no std::move or std::forward?

I have tried simple types like int, float and char as three of the parameters, but keep getting error
Error: Type does not exist within the TypeList!

Re: How dangerous is this class exactly?

Posted: June 9th, 2016, 3:33 pm
by cyboryxmen
albinopapa wrote: Why do you use const rvalue references when you are trying to use move semantics, and why no std::move or std::forward?
That was a mistype. The r-value references were supposed to be non-const.
albinopapa wrote: I have tried simple types like int, float and char as three of the parameters, but keep getting error
Error: Type does not exist within the TypeList!
I think you did the same mistake I did where the first template parameter is supposed to be the type used to store the index of the enum. So by typing <int, float, char>, you are making the index an int type and the Room be able to store types of float and char.
-Zekilk

Re: How dangerous is this class exactly?

Posted: June 9th, 2016, 6:22 pm
by albinopapa
Spinning my wheels, and nothing.

Can you show an example of creating a room from start to finish?

Re: How dangerous is this class exactly?

Posted: June 10th, 2016, 3:47 am
by cyboryxmen
Here is a test I did in main.cpp

Code: Select all

void main()
{
	using Room = shared::Room<std::size_t, int, unsigned, short, char, float, double>;
	Room room;
	room.CheckIn(int(5));

	if (room.GetCurrentOccupant() == Room::EnumClass::IndexOf<double>::index)
	{
		std::cout << room.Visit<double>() << std::endl;
	}
	if (room.GetCurrentOccupant() == Room::EnumClass::IndexOf<int>::index)
	{
		std::cout << room.Visit<int>() << std::endl;
	}

	room.CheckIn<double, true>(double(12));

	if (room.GetCurrentOccupant() == Room::EnumClass::IndexOf<double>::index)
	{
		std::cout << room.Visit<double>() << std::endl;
	}
	if (room.GetCurrentOccupant() == Room::EnumClass::IndexOf<int>::index)
	{
		std::cout << room.Visit<int>() << std::endl;
	}

	room.CheckIn<float, true>(float(5));

	if (room.GetCurrentOccupant() == Room::EnumClass::IndexOf<double>::index)
	{
		std::cout << room.Visit<double>() << std::endl;
	}
	if (room.GetCurrentOccupant() == Room::EnumClass::IndexOf<int>::index)
	{
		std::cout << room.Visit<int>() << std::endl;
	}

	using List = shared::TypeList<int, unsigned, short, void, char, float, double, void>;

	std::cout << "Has Duplicates: " << List::HasDuplicates::value << std::endl;
	std::cout << "Has void: " << List::HasVoid::value << std::endl;
	std::cout << "Index of void: " << List::IndexOf<void>::index << std::endl;
	std::cout << "Index of short: " << List::IndexOf<short>::index << std::endl;
	std::cout << "Index of char: " << List::IndexOf<char>::index << std::endl;
	std::cout << "Index of int: " << List::IndexOf<int>::index << std::endl;
	std::cout << "Index of long: " << List::IndexOf<long>::index << std::endl;
	std::cout << "Index of long long: " << List::IndexOf<long long>::index << std::endl;
	std::cout << "Index of float: " << List::IndexOf<float>::index << std::endl;
	std::cout << "Index of double: " << List::IndexOf<double>::index << std::endl;
	std::cout << "Index of long double: " << List::IndexOf<long double>::index << std::endl;
	std::cout << "Index of bool: " << List::IndexOf<bool>::index << std::endl;
	std::cout << "Index of wchar_t: " << List::IndexOf<wchar_t>::index << std::endl;
	std::cout << std::endl;

	shared::EnumClass<std::size_t, int, unsigned, short, char, float, double>  enum_class1;
	shared::EnumClass<std::size_t, float, double, long double>  enum_class2;

	enum_class1.Set<int>();
	enum_class2.Set<float>();

	std::cout << "Value of enum: " << enum_class1.GetEnum() << std::endl;

	enum_class1 = enum_class2;

	std::cout << "Value of enum: " << enum_class1.GetEnum() << std::endl;

	system("pause");
}
-Zekilk

Re: How dangerous is this class exactly?

Posted: June 10th, 2016, 5:50 am
by cyboryxmen
P.s. Do take note that the typelist in the test here is bugged. This is because the typelist terminates on void so typing void into the list can terminate it prematurely. The typelist automatically adds void to the ends of the list anyway so there is no need to type void in one of its template parameters.

Re: How dangerous is this class exactly?

Posted: June 15th, 2016, 7:50 pm
by albinopapa
Wow, a lot of CppCon 2015 was over metaprogramming. Have been watching a few videos on Youtube covering last years CppCon and my brain hurts. I don't think I'll be getting into template metaprogramming...ever lol.

What you have is quite impressive considering the complexity of metaprogramming.