Friday, September 23, 2005

Humbling Syntax

Work has been a bit slow lately, mainly because I have had to wait for other people to complete some tasks before I have much to do. Unlike some of the people discussed here, even when I "waste time" at work, I try to find something I can justify its benefits to my employer. So I spent time this week trying to learn more about the C++ Standard Template Library with the help of The C++ Programming Language. I actually own two copies of this book, one being an old printing from when I took Data Structures in college, and a more recent hardback printing. I use the old copy at work, and the new one at home. The old one had the following code:

void binary(int i)
{
    bitset<8*sizeof(int)> b = i;
    cout << b.template to_string<char>() << '\n';
}

Which even Stroustrup says:
    Unfortunately, invoking a an explicitly qualified member template requires a rather elaborate and rare syntax.


I found this rather humbling, as this isn't something I would have even recognized as legal standard C++. But wait, it doesn't compile. After deciphering error messages, it appeared Microsoft had omitted default template parameters for the character traits and allocator. So, I added the template parameters making the code look like this:


void binary(int i)
{
    bitset<8*sizeof(int)> b = i;
    cout << b.template to_string<char, char_traits<char>, allocator<char> >() << '\n';
}

After getting a successful compile, I tried googling missing template default parameters in <bitset>, but didn't find anything. As I say down to start this blog post, I grabbed my newer version of the book, and sure enough, the first code snippet had been replaced with the second. I am not sure if it was a printing error in the earlier edition or if changes had been made to the STL. I am not sure I care, either. But I do take comfort in knowing that the Visual Studio .NET 2003 STL implementation is correct in this regard.

0 Comments:

Post a Comment

<< Home