The Decltype of c++11

Source: Internet
Author: User

In C + +, DecltypeAs an operator, used to query the data type of an expression. Introduced in the C++11 standard, Decltype is designed primarily for generic programming to solve problems in generic programming that are difficult (and even impossible) to represent because some types are determined by template parameters. Generic programming has become more prevalent throughout the 1990, and the need to implement type derivation mechanisms has emerged. For this reason, many compiler vendors have implemented such operators on their own, based on the existing functions of the program language, such as TypeOf, and some implementations that have limited functionality but are easier to migrate. 2002, than Bjarne Stroustrup proposed to standardize such operators in C + + and add them to C + +, and recommend "Decltype" to reflect their ability to get the expression "claim type" (declared type). Semantically, Decltype is designed for general-purpose library writers and novice programmers. On the whole, for the target object or function, the type deduced by Decltype can exactly match the definition in the source code. As with the sizeof operator, Decltype does not need to evaluate the operand.Chinese nameOperatorForeign namesDecltypeUsequery expressionsSubstanceData Type Catalog

1 Catalog

2 Design Ideas

3 semantics

4 availability

5 examples

1 directory editing
    • 1 Design Ideas
    • 2 semantics
    • 3 availability
2 design concept Editor With the introduction of templates in C + + and the emergence of generic programming led by the Standard Template Library, the need to implement a mechanism to get the type of an expression arises, often called typeof. In generic programming, if a type is determined by a function parameter, it is often not easy to know, operationally when it is necessary to get the return type instantiated by the function template. Many vendors provide the typeof operator in the form of compiler extensions to meet this requirement. As early as the 1997, when C + + was not fully standardized, Blaine Park (Brian Parker) proposed a portable solution based on the sizeof operator. In this case, Bill Gibbons Bill Gibbons that there are still many limitations to this scheme, and that, in general, the direct introduction of the typeof mechanism works better. In October 2000, Andre Alexandrescu commented in the IT technology journal Dr. Dobb's Journal that "(if) there are typeof (operators), it is much easier to write and understand template code." He also mentions that TypeOf and sizeof have the same backend, (this is) because sizeof has to calculate the type anyway. "Andrew Konig and Barbara · E. (Barbara E. Moo) also spoke of the usefulness of the TypeOf functionality built into the programming language, but cautioned that "use often introduces some hard-to-find procedural errors and there are unresolved problems (i.e. not universal)." It is also suggested that the use of type conversions, such as the typedef provided by the Standard Template Library, can be used to achieve this function more effectively and more generally. However, Steve Danster (Steve Dewhurst) called the conversion "expensive to design and publish", and "it's simpler to take a straightforward approach to extracting expression types." "In 2011, in an article on c++0x, König and Moses prophesied:" Decltype will be widely used to facilitate the daily programming. "2002,  than Bjarne Stroustrup proposes to expand the C + + programming language to introduce a query expression type and a mechanism to initialize the object without having to indicate the type. Straustrup Note that there may be a problem with the "reference Discard" (reference-dropping) semantics provided by TypeOf in the GCC and EDG compilers, and it is difficult to understand the use of an operator that is based on expression Lvalue and returns a reference type. Thus, in the initial proposal presented to the C + + Standards Committee, the two implementations were combined: the operator returns a reference type only if the declaration type of the expression contains a reference. To emphasize that the deduced type can indeed reflect the declaration type of the expression, the proposal proposes to name the operator Decltype. The proposal also mentions the DeclOne of the main design intentions of type is to make it possible to write perfect forwarding functions. Programming, programmers sometimes need to write a generic forwarding function, regardless of the type of instantiation, can return the same type as the wrapper function, without the decltype operator, it is almost impossible to do this. The sample code for Decltype is shown below, which takes advantage of the return type back-up (trailing-return-type) syntax in the C++11 standard. int& foo (int& i); float foo (float& f); Template <class t> Auto Transparent_forwarder (t& t) −> Decltype (foo (t)) {return foo (t); Decltype is a core part of this code that holds the message that the wrapper function returns a reference type.3 semantic editing Similar to the sizeof operator, Decltype does not need to evaluate its operands. Roughly speaking, Decltype (e) is deduced as follows before returning a type:
    1. If the expression e points to a local variable, namespace scope variable, static member variable, or function parameter, then the return type is the "declared type" of the variable (or parameter);
    2. If E is an lvalue (Lvalue, or "addressable value"), then Decltype (e) will return T&, where T is the type of E;
    3. If E is an X value (Xvalue), the return value is t&&;
    4. If E is a pure right value (prvalue), the return value is T.
These semantics are designed to meet the requirements of the General library writer, but because the return type of decltype always matches the definition type of the object (or function), this is more intuitive for novice programmers. More formally, rule 1 applies to non-parenthesized identifier expressions (id-expression) and class-member access expressions. Examples are: const int&& foo (); const int bar (); int i;struct A {double x;}; CONST * A = new A ();d Ecltype (foo ()) x1; The type is const Int&&decltype (bar ()) X2; The type is Intdecltype (i) X3; The type is Intdecltype (a->x) x4; The type is Doubledecltype ((a->x)) X5; The type const double& is visible by the last two calls to Decltype, and the returned results are different. This is because the parenthesized expression (a->x) is neither an "identifier expression" nor a class-access expression, so it does not point to a named object but an lvalue, so the deduction type is "a reference to the expression type", that is, the const double&. In December 2008, Jacques Jarvi (Jaakko Järvi) pointed out to the standards committee a question: in C + +, "with qualified identifiers" (Qualified-id) cannot be made by Decltype, and this is with "Decltype (e) can be" The type definition name ' (typedef-name) treats "is inconsistent in the original design. In commenting on the formal draft prepared by the Standards Committee for C++0X (C++11), the members of the Japanese ISO member mentioned that "a domain operator (::) is not applicable to Decltype, but should have been applied." This is useful if you want to get the member type (nested type) from the instance, as shown below ":vector<int> V;decltype (v):: Value_type i = 0; int i = 0; This problem, along with other similar issues (about Decltype cannot be used in derived class declarations and destructor calls), is handled by David Vanderwold (David Vandevoorde) and voted into the work schedule in March 2010.4 Usability edits Decltype is included in the current C + + standard c++11 and is provided by many compilers as extensions: Microsoft provides the Decltype operator in the Visual C + + 2010 compiler, which basically implements the semantics described in the Standard Committee proposal, And can be used in managed code or native code. According to its documentation, this implementation "is primarily useful for developers writing template libraries." "Starting with version 4.3 released on March 5, 2008, the Gccc++ compiler also added the decltype operator. This operator is also included in the CodeGear C + + Builder 2009, Intel C + + compiler and clang. [1]5 Example editions #include <algorithm> #include <iostream> #include <iterator> #include <ostream> #include < string> #include <utility> #include <vector>using namespace std;struct Plus {Template <typename T, TypeName U>auto operator () (t&& T, u&& U) const-> decltype (forward<t> (T) + forward<u> ( u) {return forward<t> (T) + forward<u> (u);}}; int main () {vector<int> I;i.push_back (1), I.push_back (2), I.push_back (3);vector<int> J;j.push_back (40); J.push_back (j.push_back);vector<int> k;vector<string> s;s.push_back ("cut"); S.push_back ("flu") S.push_back ("Kit");vector<string> t;t.push_back ("E"); T.push_back ("Ffy"); T.push_back ("tens");vector< String> U;transform (I.begin (), I.end (), J.begin (), Back_inserter (k), Plus ()), transform (S.begin (), S.end (), T.begin (), Back_inserter (U), Plus ()), For_each (K.begin (), K.end (), [] (int n) {cout << n << ";}); cout << Endl;for_each (U.begin (), U.End (), [] (const string& r) {cout << R << "";}); cout << Endl;} Results: 63cute Fluffy kittens If in c++98, you have to pass the template parameter type to invoke the plus<int> ()And plus<string> (), the element type is repeatedly declared once. [2]
Resources

The Decltype of c++11

Related Article

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.