Address: http://www.cnblogs.com/TianFang/archive/2008/09/24/1298344.html
Boost conversion functions (1)
The boost conversion function supplements and extends the four types of conversion functions (const_cast, reinterpret_cast, static_cast, and dynamic_cast) in C ++. Before reading this article, familiarize yourself with the four types of conversion functions in C ++.
C ++ provides dynamic_cast to implement type conversion during runtime, but if it is used to convert pointers, remember to check the returned values (this is a lotProgramIf the conversion fails, a null pointer is obtained, which is a time bomb for the program.
Boost polymorphic_cast adds a detection of the return value based on dynamic_cast. If the conversion fails, it will throw a STD: bad_cast exception. The function is as follows:
Template < Class Target, Class Source>
Inline Target polymorphic_cast (source * x boost_explicit_default_target)
{
Target TMP = Dynamic_cast <Target> (X );
If (TMP = 0) Throw STD: bad_cast ();
Return TMP;
}
Although throwing an exception increases the overhead, it is easier to use.
Throwing an exception will reduce the program efficiency, and dynamic_cast will query a type_info structure to determine the correct type. Therefore, both the space cost and the time cost will greatly increase. In some application scenarios, you only need to perform type conversion during compilation. In this case, we can useStatic_castTo implement type conversion during compilation,Static_castPossible error type conversion:
Struct A
{
Virtual ~ A (){}
};
Class B: Public A {};
Class C: Public A {};
Int Main ()
{
A * pA = New C ();
B * pb = Static_cast <B *> (PA );
}
Although there is no inheritance relationship between Pa and Pb for the above programs, this conversion can be passed, and no errors will be reported during the runtime. Once petabytes are accessed, the program may even die if the program fails.
Polymorphic_downcastFirst, let's take a look at its definition:
Template < Class Target, Class Source>
Inline Target polymorphic_downcast (source * x boost_explicit_default_target)
{
Boost_assert ( Dynamic_cast <Target> (x) = X ); // Detect logic error
Return Static_cast <Target> (X );
}
From its definition, we can see that in the running release modeStatic_castThe same, that is, its release version has the sameStatic_castThe same overhead. However, in debug mode, it performs a dynamic conversion first. Once the types do not match, an exception is thrown.
In the above program, ifPolymorphic_downcastTo replaceStatic_castWe can run the program in debug mode first. If there is an incorrect type conversion, it will be easily detected. After all the errors are corrected, release the release version. In this way, there is no overhead caused by dynamic conversion and incorrect type conversion is eliminated.
End
The boost conversion function supplements and extends the four types of conversion functions (const_cast, reinterpret_cast, static_cast, and dynamic_cast) in C ++. Before reading this article, familiarize yourself with the four types of conversion functions in C ++.
C ++ provides dynamic_cast to implement type conversion during runtime, but if it is used to convert pointers, remember to check the returned values (this is something many programmers can easily forget). Otherwise, once the conversion fails, to get a null pointer is tantamount to setting a time bomb for the program.
Boost polymorphic_cast adds a detection of the return value based on dynamic_cast. If the conversion fails, it will throw a STD: bad_cast exception. The function is as follows:
Template <Class Target, Class Source>
Inline Target polymorphic_cast (source * x boost_explicit_default_target)
{
Target TMP = Dynamic_cast <Target> (X );
If (TMP = 0) Throw STD: bad_cast ();
Return TMP;
}
Although throwing an exception increases the overhead, it is easier to use.
Throwing an exception will reduce the program efficiency, and dynamic_cast will query a type_info structure to determine the correct type. Therefore, both the space cost and the time cost will greatly increase. In some application scenarios, you only need to perform type conversion during compilation. In this case, we can useStatic_castTo implement type conversion during compilation,Static_castPossible error type conversion:
Struct A
{
Virtual ~ A (){}
};
Class B: Public A {};
Class C: Public A {};
Int Main ()
{
A * pA = New C ();
B * pb = Static_cast <B *> (PA );
}
Although there is no inheritance relationship between Pa and Pb for the above programs, this conversion can be passed, and no errors will be reported during the runtime. Once petabytes are accessed, the program may even die if the program fails.
Polymorphic_downcastFirst, let's take a look at its definition:
Template < Class Target, Class Source>
Inline Target polymorphic_downcast (source * x boost_explicit_default_target)
{
Boost_assert ( Dynamic_cast <Target> (x) = X ); // Detect logic error
Return Static_cast <Target> (X );
}
From its definition, we can see that in the running release modeStatic_castThe same, that is, its release version has the sameStatic_castThe same overhead. However, in debug mode, it performs a dynamic conversion first. Once the types do not match, an exception is thrown.
In the above program, ifPolymorphic_downcastTo replaceStatic_castWe can run the program in debug mode first. If there is an incorrect type conversion, it will be easily detected. After all the errors are corrected, release the release version. In this way, there is no overhead caused by dynamic conversion and incorrect type conversion is eliminated.