0
template <typename T, int n >
int tell_me_size(T (&) [n]){
  return n;
}

this code works fine with

int a[4];
cout<< tell_me_size(a)<<endl;

while does not work with

int n;
cin>>n;
int a[n];
cout<< tell_me_size(a)<<endl;

The later gives the error "no matching function for call to ‘tell_me_size(int [n])"

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

1 Answers1

1

According to the C++ 20 Standard (13.4.3 Template non-type arguments)

2 A template-argument for a non-type template-parameter shall be a converted constant expression (7.7) of the type of the template-parameter.

Pay attention to that variable length arrays like this

int n;
cin>>n;
int a[n];

are not a standard C++ feature.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335