|
|
Serialization
|
typedef creates an alias for an existing type. It does not create
a new type that can be used for matching either function or template parameters.
This can be shown by trying to compile the following example.
typedef int a;
void f(int x); // (1) function to handle simple integers
void f(a x); // (2) special function to handle integers of type a
int main(){
int x = 1;
a y;
y = x; // other operations permitted as a is converted as necessary
f(x); // chooses (1)
f(y); // chooses (2)
}
Since typedef doesn't create a new type, this program can't compile to code
that implements its obvious intention.
Usage of BOOST_STRONG_TYPEDEF addresses this.
#include <boost/serialization/strong_typedef.hpp>
BOOST_STRONG_TYPEDEF(int, a)
void f(int x); // (1) function to handle simple integers
void f(a x); // (2) special function to handle integers of type a
int main(){
int x = 1;
a y;
y = x; // other operations permitted as a is converted as necessary
f(x); // chooses (1)
f(y); // chooses (2)
}
The program will now compile and run as expected.
BOOST_STRONG_TYPEDEF
has been designed to be similar to the standard
typedef. So
BOOST_STRONG_TYPEDEF(primitive type, name)
will create a new type "name" which will be substitutable for the original
type but still of distinct type.
BOOST_STRONG_TYPEDEF is a macro
which generates a class named "name" wraps and instance of its
primitive type and provides appropriate conversion operators in order
to make the new type substitutable for the one that it wraps.
© Copyright Robert Ramey 2002-2004. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)