c++0x characteristics in VC10 Part 2 (3): reference to right value

Source: Internet
Author: User

This article is the third page of Part 2

Forwarding problems

When programmers don't write highly-generalized code, C++98/03 's Lvalue,rvalue, references, and templates look perfect. Suppose you want to write a fully-generalized function outer (), which is designed to pass any number of arguments of any type (that is, "forward") to the function inner (). There are many good solutions, such as the Factory function make_shared<t> (args) is the constructor that passes args to T, and then returns to Shared_ptr<t>. (This means that the T object and the code used to reference it are stored in the same dynamic memory, performance is as good as an intrusive reference count), whereas a wrapper class like Function<ret (args) > is a Function object (functor) that passes parameters to its internal storage. Wait a minute. In this article, we are only interested in how the outer () is passed to the inner () part of the argument. As for the return type of the outer () it is a different question (sometimes very simple, such as make_shared<t> (args) always return to Shared_prt<t>), but to fully handle the generalization of the problem, you need to use C++0x's decltype characteristics).

If there is no parameter, there is no such problem, then with a parameter case? Let's try to write a outer ():

template <typename T> void outer(T& t) {
    inner(t);
}

The problem is, if the parameters passed to it are very rvalue, then we can't call outer (). If inner () receives the const int& parameter, the inner (5) can be compiled, but outer (5) is not compiled. Because T is deduced as int, int& cannot be bound to constant 5.

Well, let's try this:

template <typename T> void outer(const T& t) {
    inner(t);
}

If the inner () receives the int& type parameter, then the law will violate the const correctness, compiles is not past.

Now, you can overload two outer () with t& and const t& parameters, which really works. When you call outer (), it's like calling inner () directly.

Unfortunately, this method is troublesome in the case of multiple parameters ("too many overloaded functions to write"). You have to overload each parameter like t1& and const T1&, t2& and const t2&, and the number of functions to overload increases exponentially. (VC9 SP1 's Tr1::bind () is desperate enough that it overloads 63 functions for 5 parameters. If it's not so reckless, without the lengthy ravlue here, it's hard to explain to the user why we can't call a function with a parameter like 1729. In order to produce these overloaded functions using a sickening preprocessing mechanism, it's disgusting that you don't want to know about it.

In c++98/03, forwarding problems are serious and inherently stateless (you have to resort to disgusting preprocessing mechanisms that can severely slow down compilation and make your code unreadable). Finally, rvalue solved the problem gracefully.

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.