gcc, clang, and msvc all reject the following code:
#include <memory>
#include <vector>
int main() {
auto _ = std::vector<int const>{}; // error
auto _ = std::vector<int volatile>{}; // error
auto _ = std::vector<int&>{}; // error
}
See also https://godbolt.org/z/3GY9E66xh
This is because std::allocator<T>
doesn't accept T
if it is qualified by const
, volatile
, or reference. I just wonder:
Is it required by the C++ standard that T
must not be qualified by
const
, volatile
, or reference in std::allocator<T>
?
Excerpts from the lastest C++ standard n5008 (emphasis mine):
Section 16.4.4.6.1/p2 [allocator.requirements.general]
- T, U, C denote any cv-unqualified object type,
- X denotes an allocator class for type T,
Section 6.8.1/p8 [allocator.requirements.general]
An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not cv void.
For std::allocator<T>
, T
must be cv-unqualified object type, and an object type must not be a reference type. So we can say:
The C++ standard explicitly requires the template parameter type T
in std::allocator<T>
must not be qualified by const
, volatile
, or reference.