I have a Two Test classes that use variadic templates but I made the constructors explicit so that the compiler doesn't convert anything and leave everything as it was.
One class was Array and the other was called 2DArray. When I intialize 2DArray, the compiler was converting its arguments to the ones that Array was intialized with so explicit stopped this and now it strictly requires Array as the arguments. Example:
class TestClass //Array of ints.
{
void Insert(){};
template<typename T, typename... Args>
TestClass& Insert(T FirstInt, const Args... RemainingInts)
{
cout<<(FirstInt);
Insert(RemainingInts...);
return *this;
}
public:
explicit TestClass(){};
TestClass(initializer_list<int> T)
{
initializer_list<int>::iterator it = T.begin();
for (; it != T.end(); ++it)
cout<<*it;
}
template<typename T, typename... Args>
explicit TestClass(T FirstInt, const Args... RemainingInts)
{
cout<<(FirstInt);
Insert(RemainingInts...);
}
~TestClass(){};
};
class TestArrayClass //Array of array of ints.
{
void Insert(){};
template<typename T, typename... Args>
TestArrayClass& Insert(T FirstArray, const Args... RemainingArrays)
{
cout<<(FirstArray);
Insert(RemainingArrays...);
return *this;
}
public:
explicit TestArrayClass(){};
TestArrayClass(initializer_list<TestArrayClass> T)
{
initializer_list<TestArrayClass>::iterator it = T.begin();
//Push_back all Values onto a vector<Test>
}
template<typename T, typename... Args>
explicit TestArrayClass(T FirstArray, const Args... RemainingArrays)
{
cout<<(FirstArray);
Insert(RemainingArrays...);
}
~TestArrayClass(){};
};
Now if I remove the constructor for the initializer_list, I cannot initialize the class like:
Test Foo = {1, 2, 3, 4, 5}
because it said Test from initializer_list would use explicit constructor and refused to compile. If I remove the explicit, then TestArray can be initialized like:
TestArrayClass FooArray = TestArrayClass(1, 2, 3, 4)
instead of TestArrayClass(Test(1, 2, 3), Test(2, 4, 5));
Which I do not want but at the same time, it compiles perfectly fine without the explicit there when using the initialization list.
So why does this happen? I decided that to fix it I'd just provide a constructor with an initialization list as the parameters but I really don't see the need for that since it has Variadic templates :S What causes it to not compile/initialize when explicit is there? And is there any advantage to variadic templates vs. initializer_list besides being type safe? (I read that).
Oh and finally why does it allow initialization like: Test Foo{1, 2, 3}
but not Test Foo = {1, 2, 3}
??