Analysis of C ++ typeof Basic Application Methods

Source: Internet
Author: User

The C ++ programming language can be seen as an upgraded version of the C language. It not only has various features of the C language, but also makes some improvements in some specific aspects, which greatly improves the development efficiency of developers in programming. Here we will explain in detail how to apply C ++ typeof.

I. Basic usage.

1. C ++ typeof expression.

The following lists the usage:

A. Type of the expression value:

 
 
  1. template< class A, class B> 
  2. function operator*(A a, B b) -> typeof(a*b); // return type last  
  3. // big change: function keyword  
  4. // : and return are obvious alternatives for -> 
  5. template< class A, class B> 
  6. typeof(a*b) operator* (A a, B b) ; // “lookahead parsing” 

Typeof (a * B) does not really calculate the value of a * B, but returns the type of calculation result.

B. Type of the expression:

 
 
  1. template< class A, class B> 
  2. typeof(A*B) operator*(A a, B b); // use typenames  
  3. // not general  
  4. template< class A, class B> 
  5. typeof((*(A*)0)*(*(B*)0)) operator*(A a, B b); // hack 

Currently, only the gcc compiler supports this keyword.

2. auto keyword.

In the old syntax, auto identifies the storage type, indicating that this variable is valid within the range of this code block. This is the default attribute and can be left empty, so it feels a little redundant.

In c ++ 0x, auto has a new usage, which is similar to C ++ typeof. Its usage is as follows:

 
 
  1. template< class A, class B> typeof(a*b) operator*(A a, B b)  
  2. {  
  3. auto x = a*b; // avoid replication of expression/type  
  4. // …  
  5. return x;  
  6. }  
  7. auto glob = x*y; 

It seems that its function is to save code and can be replaced by C ++ typeof.

Ii. Use.

From the basic usage above, you can easily handle different types of computing problems that were hard to handle in the past.

It can also be used in other scenarios. For example, a problem I encountered some time ago, such as the following common class:

 
 
  1. struct A  
  2. {  
  3. int test ();  
  4. }; 

In another class, you want to create A delegate compatible with the: test type (custom delegate class ):

 
 
  1. struct B  
  2. {  
  3. Delegate < int()> test;  
  4. }; 

It seems very easy to complete. But if my class A has 10 methods, should I define 10 delegates in Class B? In addition, it must deal with possible changes in Class. So I used macros to help generate class B:

 
 
  1. BEGIN_DEFINE(B) // struct B{  
  2. METHOD(A,test) // Delegate< ...> test;  
  3. ...  
  4. END_DEFINE() // }; 

A method macro is the definition of a delegate based on the classes and methods specified in the parameter. How to obtain the: test type?

A: test itself is an uncertain type in C ++, unless it is static, and & A: test is A pointer value. Or that question, how can we get its type?

Trying to use the traits class template to separate types? Note that template parameters must be provided for a template class. Only template functions can export the type of template parameters themselves, and functions cannot be declared in the class.

Well, I don't need to talk much about it. Actually, using C ++ typeof is a simple implementation method:

 
 
  1. Template <class T>
  2. Class TypeTraits
  3. {
  4. TypeTraits (); // not implemented
  5. };
  6. // One of Multiple Special versions
  7. Template <class T, class Ret>
  8. Struct TypeTraits <Ret (T: *) ()>
  9. {
  10. Typedef Delegate <Ret ()> type;
  11. };

The above METHOD can be defined:

 
 
  1. #define METHOD(T,x) TypeTraits< typeof(&T::x)>::type x; 

This is the only method I have found. Record it first. The preceding section describes the specific application methods of C ++ typeof.

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.