|
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