How to tell if a pointer is the pointer to an array?

  • A+
Category:Languages

Consider this

template <class T> inline constexpr bool found_to_be_array (T* specimen) {     if constexpr (std::is_array_v<T>) {         return true;     }     else {         return false;     }; } 

The question is in which context is this going to produce the reliable result?

Clarification: I can not change the footprint of this function. I have no answer, so let me post my findings here. Consider this:

int ia[]{ 1,2,3,4,5,6,7,8,9,0 }; int iam[3][3][3][3][3]{};  // returns false auto is_array_1 = found_to_be_array(ia);  // returns true auto is_array_2 = found_to_be_array(iam); 

I am still researching this, but for multidimensional arrays, found_to_be_array works.

https://godbolt.org/g/ij73Z4

 


No this approach will not work. The parameter specimen has already decayed to a pointer type (or it might even have been a pointer all along); the metaprogramming technique used by std::is_array does not trace back in some way to the caller.

And besides, you're testing (unintentionally?) T rather than T* - but changing to the latter will not work.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: