Boost logo

Boost :

Subject: [boost] unsigned long vs unsigned int vs std::size_t (32b platform)
From: Christoph Mathys (eraserix_at_[hidden])
Date: 2009-09-17 11:26:12


Hello!

First: I'm working on 32b Linux (gcc 4.2.4) and 32b XP (Visual Studio 9 SP1).

I tried to switch from stdint.h to boost/cstdint.hpp, but had to find
out that this is not as straight forward as I thought. The compilers
handle 'unsigned int' and 'unsigned long' as different types, even
though on 32b it is AFAIK the same type. This by itself is OK, but gcc
and VS9 seem to disagree in where it comes to defining std::size_t.
The result is that the code at the end compiles on visual studio, but
not on gcc:
asdf.cpp: In function ‘void someFunc(size_t)’:
asdf.cpp:6: error: redefinition of ‘void someFunc(size_t)’
asdf.cpp:5: error: ‘void someFunc(uint32_t)’ previously defined here
asdf.cpp: In function ‘int main(int, char**)’:
asdf.cpp:16: error: call of overloaded ‘someFunc(long unsigned int&)’
is ambiguous
asdf.cpp:4: note: candidates are: void someFunc(uint16_t)
asdf.cpp:5: note: void someFunc(uint32_t)

In the stdint.h we used for visual studio previously, this error did
not occur because uint32_t was typedefd to 'unsigned __int32'. Now,
the questions are:
- should boost::uint32_t be typedefed to the same thing that
std::size_t is on 32b platforms?
- Is the size of std::size_t fixed or dependant on the platform it is used on?
- Did I do something wrong and there is a much better way of doing it
avoiding those problems in the first place?

#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>

void someFunc(boost::uint16_t) { }
void someFunc(boost::uint32_t) { }
void someFunc(std::size_t) { }

int main(int, char**)
{
        BOOST_STATIC_ASSERT(sizeof (unsigned long) == sizeof (unsigned int));
        BOOST_STATIC_ASSERT(sizeof (unsigned long) == sizeof (std::size_t));
        unsigned long ul = 42;
        unsigned int ui = 1337;
        std::size_t st = 2;

        someFunc(ul);
        someFunc(ui);
        someFunc(st);

        return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk