My following code is used to check if an element exists in an array of the same type:
#include <iostream>
#include <vector>
using namespace std;
template <class T>
bool IsIn (T a, T B[]) {
bool result = false;
int _size = sizeof(B)/sizeof(B[0]);
for(int i = 0; i < _size; i++)
if (a == B[i])
result = true;
return result;
}
int main () {
int arr1 [] = { 10, 20, 30 };
bool ee1 = IsIn(10, arr1);
cout << "ee1 = " << ee1 << endl;
return 0;
}
I run it here using the C++
option, and get the following warnings:
main.cpp: In instantiation of ‘bool IsIn(T, T*) [with T = int]’:
main.cpp:19:27: required from here
main.cpp:9:15: warning: ‘sizeof’ on array function parameter ‘B’ will return size of ‘int*’ [-Wsizeof-array-argument]
9 | int _size = sizeof(B)/sizeof(B[0]);
| ^~~~~~~~~
main.cpp:7:19: note: declared here
7 | bool IsIn (T a, T B[]) {
| ~~^~~
What have I done wrong? I don't quite get the meaning of these messages. Thank you in advance!