C++ TR1: stdint.h still missing from Visual Studio

stdint.h is a disappointing absence from both Visual Studio 2008 and the TR1 feature pack. This header was introduced in the C99 standard library to allow programmers to write more portable code by providing a set of typedefs that specify exact-width integer types, together with the defined minimum and maximum allowable values for each type. This standardized the approach for writing such things as uint32_t and should save countless hours of needless duplication for projects that like to define their own variants such as uint32, UINT32, u32, Xyz32U, etc.

Since the C++ standard was finalized in 1998, it missed this standard header by a year. In the latest update to the C++ standard (namely those extensions covered in TR1), support for this header has been added in the form of the cstdint header.

It is astonishing to find that a header that was standardized 9 years ago has still not made its way into Visual Studio 2008. Not even the recent feature pack beta which included support for most of the TR1 extensions, contained stdint.h! The lack of support for this header was logged as a bug with Microsoft way back in 2005 but is still in the “postponed” bucket.

Thankfully, there are a number of implementations of stdint.h available, the most notable being Paul Hsieh’s cross-platform free implementation and also a Microsoft compiler specific implementation. Simply place one of these implementations into the Visual Studio standard include paths and stdint.h support magically appears … now why can’t Microsoft do something like that!

Posted in C++

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

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:

#include
#include using namespace std;
using namespace std::tr1;

int main()
{
    array arr = {1, 2, 3, 4};
    cout << "size: " << arr.size() << endl;     cout << "max_size: " << arr.max_size() << endl;     return 0; }[/sourcecode]

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

size_type max_size() const
{
    // return maximum possible length of sequence
    size_type _Count = (size_type)(-1) / sizeof(_Ty);
    return (0 < _Count ? _Count : 1); }[/sourcecode] 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.

 
 
 
 
 

 

 

C++ TR1

Effective C++, Third Edition summarizes TR1 this way:

TR1 (“Technical Report 1”) is a specification for new functionality being added to C++’s standard library. This functionality takes the form of new class and function templates for things like hash tables, reference-counting smart pointers, regular expressions, and more. TR1 itself is just a document.

The TR1 draft does not contain any background information on the functionality it provides and doesn’t contain any examples for how it should be used. For this sort of information refer to the proposal documents which were used to define the TR1 functionality. The relevant proposal documents are nicely catalogued by Scott Myers.

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.

GCC v4.x also provides support for most of the extensions.

Over the next few weeks (more likely months) I plan on playing with the new TR1 features and I will add thoughts, learnings and code snippets to this blog.

Posted in C++

Simple container dump using STL iterator

Quick and dirty printing of containers contents in C++ using STL ostream_iterator …

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    vector<int> v(10);
    generate(v.begin(), v.end(), rand);
   
    copy(v.begin(), v.end(),
        ostream_iterator<int>(cout, "n"));

    return 0;
}