There is an answer on SO which explains how to perform a search for a value in an array. Coming from a PHP background, I am used to a degree of dynamic typization - something not present in C++, which then poses several challenges. Would it be then possible to create a helper function that would perform analogous to PHP's in_array($needle, $haystack)
(for example, using the code from the linked answer) to be used as a shorthand?
Having written this snippet, I understand (tangibly) why it doesn't work - the parameters don't really have types signified. What, if anything, could be done to circumvent this, and would it be bad practice to do so?
bool in_array(needle, haystack) {
// Check that type of needle matches type of array elements, then on check pass:
pointer* = std::find(std::begin(haystack), std::end(haystack), needle);
return pointer != std::end(haystack);
}
Edit: To be extra clear, I don't really want to PHPize C++ - what I was looking for is a way it's usually done in C++!