samedi 25 novembre 2017

class template's constructor with parameter pack giving C3520 error

Experimenting with some templates and their uses:

I was working on this simple struct here:

template<typename T, size_t n>  // templated but not variadic
struct myArray {
    static const size_t SIZE = n;
    T arr_[SIZE];

    myArray() {}   // Default Constructor

    template<class... U>      // Initialization Constructor
    myArray( U pack... ) {    // Templated with variadic parameters of
                              // type U = T upto SIZE = n;
        arr_ = pack...;
    }
};

I would like to use this in this manner or similar:

int main() {
    myArray<char, 6> arr1{ 'a', 'b', 'c', 'd', 'e', 'f' };
    myArray<int, 4> arr2{ 1, 2, 3, 4 };      

    // Or like this:
    myArray<char, 6> arr1 = { 'a', 'b', 'c', 'd', 'e', 'f' };
    myArray<int, 4> arr2 = { 1, 2, 3, 4 };
}

And in visual studio 2017RC I keep getting this compiler error:

1>------ Build started: Project: PracticeMath, Configuration: Debug Win32 ------
1>PracticeMath.cpp
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(19): error C3520: 'U': parameter pack must be expanded in this context
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(22): note: see reference to class template instantiation 'myArray<T,n>' being compiled
1>c:\users\skilz80\documents\visual studio 2017\projects\practicemath\practicemath\practicemath.cpp(41): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Done building project "PracticeMath.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Not sure what I am missing here or what to do within the initialization constructor.

Aucun commentaire:

Enregistrer un commentaire