Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015: is a set of const pointers allowed?

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.

like image 939
Houbie Avatar asked May 09 '16 08:05

Houbie


People also ask

Can we increment constant pointer?

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.

Can a const pointer be modified?

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.

What is the use of const 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.


2 Answers

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.

like image 171
cadaniluk Avatar answered Sep 21 '22 12:09

cadaniluk


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}};
like image 26
uh oh somebody needs a pupper Avatar answered Sep 21 '22 12:09

uh oh somebody needs a pupper