Research and modification of Boost: trait: is_reference

Source: Internet
Author: User
Research and modification of Boost: trait: is_reference
Http://www.cppblog.com/yindf/archive/2009/02/20/74452.html

Let's take a look at the implementation of boost.

1 template <typename _ T>
2 struct wapper
3 {};
4 template <typename _ T>
5 _ T & (* fun1 (wapper <_ T> T ))();
6 true_type fun1 ();
7
8 class true_type {};
9 class false_type
10 {
11 char C [8];
12 };
13
14 template <typename _ T>
15 true_type fun2 (_ T &(*)());
16 false_type fun2 ();
17
18 template <typename _ T>
19 struct is_reference
20 {
21 static const bool value = sizeof (fun2 (fun1 (wapper <_ T> () = sizeof (false_type );
22 };

This is what it looks like. I simplified it to make it easier to understand.

The following is my implementation version.

1 template <typename _ T>
2 class is_reference
3 {
4 template <typename _ T>
5 struct wapper
6 {};
7
8 class true_type {};
9 class false_type
10 {
11 char C [8];
12 };
13
14 template <typename _ T>
15 static _ T & fun1 (wapper <_ T> );
16 static true_type fun1 ();
17
18 template <typename _ T>
19 static true_type fun2 (_ t );
20 static false_type fun2 (true_type );
21 public:
22 static const bool value = sizeof (fun2 (fun1 (wapper <_ T> () = sizeof (false_type );
23 };

The usage is as follows:

1 bool RES1 = is_reference <char>: value; // RES1 = false
2 bool RES2 = is_reference <char & >:: value; // RES2 = true

Function parameters automatically remove references such:
Template <_ T> void fun (_ t );
At any time, _ t is always of a non-reference type.

However, if you do not want a function to directly derive the type of template parameters through function parameters, add an indirect layer wapper to the function parameters,
The class template does not automatically remove the reference, so you can use the function template to ensure that the original type is obtained.
 
Template <_ T> void fun (wapper <_ T> );
At this time, _ T may be of the reference type. C ++ does not support referenced references. When a referenced reference is used in a template function, the template function fails to be deduced.
That is, as long as _ T & is included in the parameter or return value of function fun, fun will fail to be deduced. Thus, the compiler selects true_type fun (...);
Because the parameters have been used to derive template parameters, they can only contain _ T & in the return type, and thus distinguish between reference and non-reference by using function overloading.
If _ T & type is directly returned, the function that only accepts true_type parameters must be defined later. Because _ T & must be of the reference type, it will be accepted later.
False_type fun2 (true_type) function will be selected.

But what should I do when I encounter is_reference <true_type>: Value? I put all of them in the private domain and will never see it.
Boost: the solution for returning function pointers in trait is also OK. Because Char will never match the function pointer.

The key to this method is that the compiler selects the order of the overloaded functions.
The boost: The method in trait is that char can never be converted into a function pointer, so as to select different overloaded versions.

The explanation is complete. Posted on Yin dongfei reading (835) Comments (3) EDIT favorites reference



Feedback: # Re: boost: trait: is_reference research and modification | good location, good reply to more comments

# Re: boost: trait: is_reference research and modification [not logged on] | jans2002 master, always think the template is very mysterious, and there is no good debugging method to reply to more comments

# Re: boost: trait: is_reference research and Modification

| Yin dongfei
@ Jans2002

The template is not easy to debug now, but it seems that the intelliisence of VC 10 will be helpful. It has never been used.

I am still pushing the template by myself. I wonder if the template is as easy as writing a program... Reply to more comments

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.