unsigned long vs unsigned int vs std::size_t (32b platform)

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; }

Christoph Mathys wrote:
void someFunc(boost::uint16_t) { } void someFunc(boost::uint32_t) { } void someFunc(std::size_t) { }
Not sure if you are optimizing for 16bit and 32bit. Wouldn't a template function be appropriate here? If you are optimizing, you can just specialize. template< typename T > void someFunc( T ) { } void someFunc(boost::uint16_t) { } void someFunc(boost::uint32_t) { } -- Thomas Suckow

Thomas Suckow wrote:
Christoph Mathys wrote:
void someFunc(boost::uint16_t) { } void someFunc(boost::uint32_t) { } void someFunc(std::size_t) { }
Not sure if you are optimizing for 16bit and 32bit.
It is actually code handling some endianness stuff.
Wouldn't a template function be appropriate here?
Maybe a template function would be more appropriate, avoiding possibly dangerous conversions by putting a static assert into the default implementation. But I'd still have to provide specializations for uint32_t and unsigned long or uint32_t and unsigned int, depending on the platform. Ideally, I'd like to work only with those fixed width integers if I need to provide an implementation for every integer type, avoiding code noise with ifdef and co. Christoph

Christoph Mathys wrote:
Thomas Suckow wrote:
Christoph Mathys wrote:
void someFunc(boost::uint16_t) { } void someFunc(boost::uint32_t) { } void someFunc(std::size_t) { }
Not sure if you are optimizing for 16bit and 32bit.
It is actually code handling some endianness stuff.
Wouldn't a template function be appropriate here?
Maybe a template function would be more appropriate, avoiding possibly dangerous conversions by putting a static assert into the default implementation. But I'd still have to provide specializations for uint32_t and unsigned long or uint32_t and unsigned int, depending on the platform.
Ideally, I'd like to work only with those fixed width integers if I need to provide an implementation for every integer type, avoiding code noise with ifdef and co.
For sure, the typedefs must be an alias for some of the built in types, so you would have to provide overloads for them - unsigned short, unsigned int, unsigned long, and (in the very near future) unsigned long long. Bo Persson

AMDG Christoph Mathys wrote:
Wouldn't a template function be appropriate here?
Maybe a template function would be more appropriate, avoiding possibly dangerous conversions by putting a static assert into the default implementation. But I'd still have to provide specializations for uint32_t and unsigned long or uint32_t and unsigned int, depending on the platform.
Ideally, I'd like to work only with those fixed width integers if I need to provide an implementation for every integer type, avoiding code noise with ifdef and co.
Would it make sense to dispatch on the size: #include <climits> #include <boost/mpl/assert.hpp> #include <boost/mpl/int.hpp> template<class T> struct bits : boost::mpl::int_<sizeof(T) * CHAR_BIT> {}; template<class T> void someFunc(T, boost::mpl::int_<64>) {} template<class T> void someFunc(T, boost::mpl::int_<32>) {} template<class T> void someFunc(T, boost::mpl::int_<16>) {} template<class T> void someFunc(const T& t) { return(someFunc(t, bits<T>())); } int main() { someFunc(1); } In Christ, Steven Watanabe

Steven Watanabe wrote:
Christoph Mathys wrote:
Wouldn't a template function be appropriate here?
Maybe a template function would be more appropriate, avoiding possibly dangerous conversions by putting a static assert into the default implementation. But I'd still have to provide specializations for uint32_t and unsigned long or uint32_t and unsigned int, depending on the platform.
Ideally, I'd like to work only with those fixed width integers if I need to provide an implementation for every integer type, avoiding code noise with ifdef and co.
Would it make sense to dispatch on the size:
Indeed it would and I was going to suggest the same. I do that in my own endianness swapping functions. By using function templates for swapping to and from network order, I can swap any types that have supported sizes. Don't forget to handle sizeof(T) == 8, if you can assume a byte is eight bits, as a special case that simply returns the output. Also note that swapping 64b types to/from network order uses the same code. That is, write it once and use it for both directions. Some platforms offer highly tuned macros or intrinsics for various sizes. When dispatching on size, you can safely select the right platform-specific implementation. When T is not a type expected by the underlying swapping code, you must convert from T to the expected type and back again. When I tested the performance of doing so, I found that the various techniques -- casting, casting among pointers, converting union, memcpy(), etc. -- had varying performance. Their relative performance varied by T, particularly by T's size, and by platform, but only slightly. Rather than getting into highly complicated and convoluted tuning logic using the preprocessor and even worse size dispatching, I just selected the converting union approach because it was the clearest to read. _____ Rob Stewart robert.stewart@sig.com Software Engineer, Core Software using std::disclaimer; Susquehanna International Group, LLP http://www.sig.com IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

In response to the other questions,
- should boost::uint32_t be typedefed to the same thing that std::size_t is on 32b platforms?
Most of the time, but doesn't have to be. std::size_t is defined to be the return type of "sizeof".
- Is the size of std::size_t fixed or dependant on the platform it is used on?
Platform Dependant -- Thomas Suckow

Christoph Mathys wrote:
The compilers handle 'unsigned int' and 'unsigned long' as different types, even though on 32b it is AFAIK the same type.
They are different types, that just happen to be the same size on some systems.
#include <boost/static_assert.hpp> #include <boost/cstdint.hpp>
void someFunc(boost::uint16_t) { } void someFunc(boost::uint32_t) { } void someFunc(std::size_t) { }
Overloading on typedefs are generally not recommended, precisely because you do not know the underlying types. Bo Persson
participants (5)
-
Bo Persson
-
Christoph Mathys
-
Steven Watanabe
-
Stewart, Robert
-
Thomas Suckow