We will be upgrading to VS2015 soon, and I found this in the breaking changes list:
const elements
The C++ standard has always forbidden containers of const elements (such as vector or set). Visual C++ 2013 and earlier accepted such containers. In the current version, such containers fail to compile.
source
I was wondering if anyone knows if this also applies to a set. I know a map can still contain const pointers as keys, since they are const anyway.
An example:
std::set<const QObject*>
Can I still do this? I would think not, according to the post on the site of Microsoft.
A pointer to constant is a pointer through which the value of the variable that the pointer points cannot be changed. The address of these pointers can be changed, but the value of the variable that the pointer points cannot be changed.
const int * is a pointer to an integer constant. That means, the integer value that it is pointing at cannot be changed using that pointer.
A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.
const QObject*
is a pointer to a const QObject
. The pointer itself is still modifiable. const QObject* const
would make the pointer itself const
.
Since the Microsoft article talks about const
objects, which const QObject*
is not, your example is fine.
I know a map can still contain const pointers as keys, since they are const anyway.
std::set<T* const>
has always been invalid, and std::map<int* const, int* const> s;
has always been valid. The reason is because the allocator for std::set
is std::allocator<Key>
, whereas the allocator for std::map
is std::allocator<std::pair<const Key, T>>
. By definition, a std::allocator<const T>
is ill-formed. If you want the map
code to fail, you'll have to specify a custom allocator like this:
int i = 42;
int* const j = &i;
std::map<int* const, int* const,
std::allocator<const std::pair<int* const, int* const>>> s{{j, j}};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With