Reputation: 13
I have a short and convenient code for multi-dimensional vectors, with the ability to choose size and default value during initialization.
#include <iostream>
#include <vector>
using std::vector;
template<int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template<typename... Args>
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template<typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
};
int main() {
int nbRow, nbCol;
std::cin >> nbRow >> nbCol;
Vec<2, int> grid(nbRow, nbCol, 0);
}
It works with GCC, but not with clang 10.0.0 (standard c++17 or gnu++17, both don't work).
main.cpp:15:25: error: missing default argument on parameter 'args'
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
^
main.cpp:30:14: note: in instantiation of function template specialization 'Vec<2, int>::Vec<int, int>' requested here
Vec<2, int> grid(nbRow, nbCol, 7);
^
main.cpp:15:25: error: missing default argument on parameter 'args'
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
Is there a simple way to fix this? Thanks in advance!
Upvotes: 1
Views: 2170
Reputation: 66991
int n = 0
an argument with a default value can't be followed by arguments without defaults. Remove the =0
Upvotes: 4