Bug #83077 Make array_elements() type-safe
Submitted: 21 Sep 2016 11:19 Modified: 27 Sep 2016 15:23
Reporter: Steinar Gunderson Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:8.0.1 OS:Any
Assigned to: CPU Architecture:Any

[21 Sep 2016 11:19] Steinar Gunderson
Description:
array_elements() is a macro defined as 

  #define array_elements(A) ((uint) (sizeof(A)/sizeof(A[0])))

This was the best you could do in the C days, but now in C++11, it's possible to make it type-safe. Specifically, the current version contains some traps where you can inadvertently apply it to a pointer:

int foo(int x[13])  // x decays from array to pointer in argument
{
  return array_elements(x);  // No error, returns 2!
}

or an STL container:

int bar(const std::vector<int> &x)
{
  return array_elements(x);  // No error, returns 6!
}

How to repeat:
N/A

Suggested fix:
We should use a constexpr template instead. "Effective Modern C++" shows how.
[27 Sep 2016 15:23] Paul DuBois
Posted by developer:
 
Fixed in 8.0.1.

Code cleanup. No changelog entry needed.