What type should std::remove_cv produce on an array of const T?
Asked Answered
V

1

5

What type should std::remove_cv<const int[3]> produce? int[3] or const int[3]?

const int[3] is an array of 3 const int right?, and has no top-level cv-qualifier. So shouldn't it produce const int[3]? Latest version of gcc/libstdc++ is producing int[3] I think. Is this a bug? Why / why not?

Villein answered 4/1, 2015 at 20:51 Comment(4)
While the behavior is sensible, I wonder how it's justified... (One could read it as a const array of 3 int.)Solitary
Effectively, it will end up using a specialization like template <typename T> struct remove_cv<T const> { using type = T; };. I don't think T const[N] would match that specialization.Vershen
@DietmarKühl: Standard says "The [result type] shall be the same as T except that any top-level cv-qualifier has been removed."Villein
open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1059Melchor
M
8

N4140 §3.9.3 [basic.type.qualifier]/p5, emphasis mine:

Cv-qualifiers applied to an array type attach to the underlying element type, so the notation “cv T,” where T is an array type, refers to an array whose elements are so-qualified. An array type whose elements are cv-qualified is also considered to have the same cv-qualifications as its elements. [ Example:

typedef char CA[5];
typedef const char CC;
CC arr1[5] = { 0 };
const CA arr2 = { 0 };

The type of both arr1 and arr2 is “array of 5 const char,” and the array type is considered to be const-qualified. —end example ]

See also CWG issue 1059.

Melchor answered 4/1, 2015 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.