Development environment: VS2005 (VC8)
The following code can not be run on VS2002, it is said that VS2002 (VC7) does not fully support the partial, VS2003 (VC7.1) support.
Questions raised:
One, the known type t,u, how to know in the compilation phase T can convert to U.
Template <class T, class u>
struct Conversionhelper
{
Static long Test (...); /WQ Note: in C + +, indeterminate parameters do not require "at least one parameter".
Static short Test (U);
Static T maket ();
};
Template <class T, class u>
struct Conversion
{
typedef conversionhelper<t, u> H;
enum {exists = sizeof (short) = sizeof (H::test (H::maket ()))};
};
Class A
{
Public
A ()
{
TRACE ("\r\nar\n");
}
};
Class B:public A
{
Public
B ()
{
TRACE ("\r\nbr\r\n");
}
};
Test code:
BOOL B1 = conversion<b,a>::exists;
BOOL B2 = conversion<a,b>::exists;
The results of the operation were true,false.
Second, answer the above question. If T can be converted to u, define an object of type U, otherwise define a variable of type T.
Template <int flag, TypeName T, TypeName u>
struct Select
{
typedef U Result;
};
Template <typename T, TypeName u>
struct select<0, T, u>
{
typedef T Result;
};
Test code:
Select<conversion<b,a>::exists,b,a>::result type;
Type is a;b no longer inherits with a, then type is B.
Read Loki, the compilation phase found that two classes can be converted