I'm currently learning about template meta-programming in c++ and stumbled across variable templates. As a fun exercise, I decided to implement compile time static array with following usage -
my_array<1,2,3,4> arr; // gives an array with 4 members = 1,2,3,4 I've tried several iterations of my attempt, removing syntax errors along the way but now I'm stuck since no compiler gives useful warning. Here's my current code -
#include <iostream> template<size_t... Enteries> constexpr size_t my_array[sizeof...(Enteries)] = {Enteries...}; int main() { my_array<1,2,3,4,5,6> arr; } but currently it gives following error with clang -
static_array.cpp:7:10: error: expected ';' after expression my_array<1,2,3,4,5,6> arr; ^ ; static_array.cpp:7:24: error: use of undeclared identifier 'arr' my_array<1,2,3,4,5,6> arr; ^ static_array.cpp:7:2: warning: expression result unused [-Wunused-value] my_array<1,2,3,4,5,6> arr; ^~~~~~~~~~~~~~~~~~~~~ 1 warning and 2 errors generated. and with gcc -
static_array.cpp: In function ‘int main()’: static_array.cpp:7:24: error: expected ‘;’ before ‘arr’ my_array<1,2,3,4,5,6> arr; ^~~ static_array.cpp:7:27: warning: statement has no effect [-Wunused-value] my_array<1,2,3,4,5,6> arr; How should I proceed forward to implement this thing(preferably with variable templates since I know this can be implemented with old struct technique).
my_array<1,2,3,4,5,6>is the array.