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.

Answers


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

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 (§17.6.3.5), 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


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.