What header file is where the boost library define its own primitive data type?
Recently, I try to use the boost::spirit::qi binary endian parser to parse some binary data depends on the endianness of the Platform. There is a simple example, like following:
Using declarations and variables:
using boost::spirit::qi::little_word; using boost::spirit::qi::little_dword; using boost::spirit::qi::little_qword; boost::uint16_t us; boost::uint32_t ui; boost::uint64_t ul;
Basic usage of the little endian binary parsers:
test_parser_attr("\x01\x02", little_word, us); assert(us == 0x0201);
test_parser_attr("\x01\x02\x03\x04", little_dword, ui); assert(ui == 0x04030201);
test_parser_attr("\x01\x02\x03\x04\x05\x06\x07\x08", little_qword, ul);
assert(ul == 0x0807060504030201LL);
test_parser("\x01\x02", little_word(0x0201));
test_parser("\x01\x02\x03\x04", little_dword(0x04030201));
test_parser("\x01\x02\x03\x04\x05\x06\x07\x08",
little_qword(0x0807060504030201LL));
It works very well. But my questions come, why do we need use some data types like boost::uint16_t
, boost::uint32_t
here? Can I use unsigned long
or unsigned int
here?
And if I want to parse double
or float
data type, what boost 开发者_如何学运维data type should I use? And please tell me where is boost define the above these types?
Types like uint16_t or uint32_t exist so that you can declare a variable to have a specific bit width. You can't do this with normal types like "long" because they are different sizes on different architectures and/or implementations. The afore mentioned types are normally derived by through preprocessor calculations resulting in typedefs to the implementation/architectural specific type to get that specific size.
The file <boost/cstdint.hpp>
contains all of the boost::(u)int(8|16|32|64)_t
definitions. This is provided mainly because MSVC does not ship with <cstdint>
. On C++ compilers that are also C compilers (like gcc) <boost/cstdint.hpp>
just imports <cstdint>
into the boost namespace.
see also: stdint.h
精彩评论