Correct behaviour for std::vector<T>::value_type

After scratching my head at some errors in some template code that used std::vector::value_type I tracked it down to the following. Is this correct behavior according to the standard, or is this a problem with MSVC 2012 CTP?

typedef std::vector<int>::value_type       t1;
typedef std::vector<int const>::value_type t2;

static_assert(!std::is_same<t1, t2>::value, "hmmm");

The above assertion fails.


The value_type of a std::vector<T> is T (§

The value of is_same takes cv qualifiers into account (§20.9.6).

In your case that means checking std::is_same<int, int const>, which is supposed to fail.

Which in turn means that the behavior you observed is wrong according to the standard. It seems that MSVC is dropping the cv qualifiers for value_type:

std::vector<const int>::value_type val = 5;
val = 10;

This compiles on MSVC2008 but fails with gcc 4.4.

You should probably file a bug report with Microsoft.

Edit: Nawaz's comment above got me thinking. According to this question const int is indeed not allowed as a a value_type in C++03! It seems though that it is in C++11.. While it's not explicitly forbidden in C++11 for vectors, it is forbidden for allocators (§, which in turn makes it illegal for vectors as well.

In any case, MSVC's behavior of silently dropping the const seems wrong here.

Need Your Help

Do you ToList()?

linq linq-to-sql

Do you have a default type that you prefer to use in your dealings with the results of LINQ queries?

InvalidOperationException with BackgroundWorker in WPF

c# wpf

I'm trying to get a BackgroundWorker to update the UI during program logic execution. But I get the error:

How to implements _isset magic method?

php magic-methods

I try to implement the __isset magic method such as the following code,