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}??

Edited 4 Years Ago by triumphost

So why does this happen?

If you try to call an explicit constructor with an implicit expression then obviously it's going to fail. That's what explicit means.

I do not see what is implicit about it? It's all integers and they are all of the same type.

Test Foo{1, 2, 3} but not Test Foo = {1, 2, 3}

With the above I might as well do Test Foo(1, 2, 3).

Mike's tutorial has:

int_vector v{1,2,3,4,5}; //construction syntax with {} enclosed lists.

int_vector v2 = {6,7,8,9,10};

Yet I cannot do the same :S

If my constructor is not explicit it works. If it is explicit it does not work unless I provide that extra constructor.

Edited 4 Years Ago by triumphost

Test Foo{1, 2, 3} but not Test Foo = {1, 2, 3}

Those are not the same thing. The first is actually an alternative to the explicit Test Foo(1, 2, 3) while the second is an implicit expression similar to Test Foo = 123 (assuming you supported Test(int) as a non-explicit constructor).

And once again assuming that Test(int) is supported, you probably already know that Test Foo(123) is not the same as Test Foo = 123. The equals sign makes all the difference in your example.

Adding the curly brace alternative to C++ was a mistake, in my opinion.

Edited 4 Years Ago by deceptikon

Comments
Ahh Thank you. Just needed that.
This question has already been answered. Start a new discussion instead.