C++ TR1: array VS 2008 Bug

Microsoft have recently released a Beta version of the Visual Studio 2008 Feature Pack which includes support for most of the C++ standard library extensions described in TR1. Alas, the beta sticker is appropriate as there are still some bugs to be ironed out.

The TR1 array container template provides functionality for implementing a fixed size array. This is a halfway point between plain old C style arrays and C++ STL vectors. The defining property of the array container is that its size is fixed. Consider and example of an array of 4 integers

?
1
array< int , 4> = { 0, 1, 2, 3 };

Since it adheres to the STL container rules, it must implement methods such as size(), and max_size(). For the array container, both of these should return the size of the array, which is fixed. In the above example, both should return 4. However, when using the VS 2008 TR1 implementation of array, a bug appears. The code:

?
01
02
03
04
05
06
07
08
09
10
11
#include <iostream>
#include <array>using namespace std;
using namespace std::tr1;
 
int main()
{
     array< int , 4> arr = {1, 2, 3, 4};
     cout << "size: " << arr.size() << endl;
     cout << "max_size: " << arr.max_size() << endl;
     return 0;
}

Produces the output:

size: 4max_size: 1073741823

instead of:

size: 4max_size: 4

If we take a look at the implementation of max_size() we can see the problem

?
1
2
3
4
5
6
size_type max_size() const
{
     // return maximum possible length of sequence
     size_type _Count = (size_type)(-1) / sizeof (_Ty);
     return (0 < _Count ? _Count : 1);
}

Instead of simply retuning N (the size of the array), it performs the same computation as if this was a vector.

This issue has been logged as a bug with Microsoft and will hopefully be fixed before the "Gold" release of the feature pack.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值