#include <iostream>
/* prints sequentially the elements of an array */
template<typename T, unsigned int _size>
void log_seq_pbr(const T (&x)[_size]){
for(unsigned int i = 0; i < _size ; i++){
std::cout << i << " " << x[i] << std::endl;
}
return;
}
/* prints sequentially the elements of an array */
template<typename T, unsigned int _size>
void log_seq_pbv(const T x[_size]){
for(unsigned int i = 0; i < _size ; i++){
std::cout << i << " " << x[i] << std::endl;
}
return;
}
int main(int argc, char const *argv[])
{
int arr[10] = {0};
log_seq_pbr(arr);
log_seq_pbv(arr);
return 0;
}
The only difference between those two funtions is .._pbr
takes its argument by reference, while .._pbv
takes its argument by value. The call to by reference one compiles fine, however, the call to by value one gives the following error:
tmp.cpp: In function 'int main(int, const char**)':
tmp.cpp:33:20: error: no matching function for call to 'log_seq_pbv(int [10])'
33 | log_seq_pbv(arr);
| ^
tmp.cpp:17:6: note: candidate: 'template<class T, unsigned
int _size> void log_seq_pbv(const T*)'
17 | void log_seq_pbv(const T x[_size]){
| ^~~~~~~~~~~
tmp.cpp:17:6: note: template argument deduction/substitution failed:
tmp.cpp:33:20: note: couldn't deduce template parameter '_size'
33 | log_seq_pbv(arr);
Why it cannot figure out _size
for pass-by-value, while it is able to do it for pass-by-reference one?