Read Loki, the compilation phase found that two classes can be converted

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.