Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ 11 feature: decltype keywords     - Java thread lifecycle (Programming)

- Spark read more HBase tables a RDD (Server)

- Zabbix system email alert Python script (Server)

- Ubuntu 14.10 / 14.04 how to install Quick Start tool Mutate 2.2 (Linux)

- Python Django model within the class meta Detailed (Programming)

- Python method to traverse folders and file read and write (Programming)

- How do you know your public IP terminal in Linux (Linux)

- iSCSI + multipath achieve DM multipath storage (Server)

- CentOS / RedHat system partition essential requirements and partition scheme (Linux)

- How to use SHA256 checksum files download (Linux)

- Normal start Lazarus 1.0.8 under Ubuntu (Linux)

- CKEditor + SWFUpload achieve a more powerful editor (Linux)

- open V switch port mirror in OpenStack neutron (Server)

- Linux System Getting Started Learning: rename multiple files in Linux (Linux)

- CentOS static IP network configuration (Linux)

- Linux system started to learn: how to solve the xxx is not in the sudoers file error (Linux)

- Configuring Eclipse Note Templates (Linux)

- How to avoid two Chrome icon appears in ELementary OS Freya (Linux)

- Port is not being used, how will bind failure? (Server)

- linux network security experience (Linux)

 
         
  C ++ 11 feature: decltype keywords
     
  Add Date : 2018-11-21      
         
         
         
  Decltype Profile

typeid operator we used before to query a variable type, this type of query at run time. RTTI mechanism for each type produces a type_info types of data, and the variable corresponding type_info typeid query returns data, the return type the name of the member function by name. While in C ++ typeid 11 also provides hash_code this member function that returns the type of unique hash value. RTTI can lead to run-time efficiency is lowered, and in generic programming, we need to determine is necessary to compile-time type, RTTI and unable to meet such requirements. Compile-time type inference for generic programming is the emergence, in the non-generic programming, our types are identified, there is no further derivation.

The compile-time type inference, in addition to the auto keyword we said, there's this article decltype.

decltype and auto keywords, used for compile-time type inference, but it still has some differences with the auto. Decltype type of type inference is not the same as auto variable declarations from initialization expression obtained variable, but always with a common expression as a parameter and return type of the expression, and will not have expressions decltype evaluated.

decltype Usage

Derived expression type

    int i = 4;
    decltype (i) a; // result is derived int. a type of int.
In combination with using / typedef, used to define the type.

    using size_t = decltype (sizeof (0)); // sizeof (a) the return value of type size_t
    using ptrdiff_t = decltype ((int *) 0 - (int *) 0);
    using nullptr_t = decltype (nullptr);
    vector vec;
    typedef decltype (vec.begin ()) vectype;
    for (vectype i = vec.begin; i = vec.end ();! i ++)
    {
        // ...
    }
Such as auto and also to improve the readability of the code.

Reuse anonymous type

In C ++, we sometimes encounter a number of anonymous types, such as:

struct
{
    int d;
    doubel b;
} Anon_s;
And with decltype, we can re-use this anonymous structure:

decltype (anon_s) as; // defines an anonymous structure above
Generic programming combined with auto, used to track the return value type

This is the largest use of decltype.

template
auto multiply (_Tx x, _Ty y) -> decltype (_Tx * _Ty)
{
    return x * y;
}
decltype derived four rules

If e is not a bracketed tag expression or class member access expression, so decltype (e) is the type of the named entity e. In addition, if e is an overloaded function will result in a compilation error.
Otherwise, assume that the type of e is T, will die if e is a value, then decltype (e) for the T &&
Otherwise, assume that the type of e is T, if e is an lvalue, then decltype (e) for the T &.
Otherwise, assuming the type of e is T, then the decltype (e) for the T.
Tag refers to the removal of the programmer tag keywords, literals and other compilers need to use outside their own definition of the mark, while a single tag is the tag corresponding expression expressions. E.g:

int arr [4]
Arr is a marker expression, and arr [3] +0 not.

Let's look at the following code:

    int i = 10;
    decltype (i) a; // a derivation of int
    decltype ((i)) b = i; // b deduced to be int &, have their initialization, otherwise the compilation error
Just as i added (), it has led to differences in type inference results. This is because, i is a marker expression, according to the derivation rule 1, it is deduced to type int. And (i) as an lvalue expression, so the type is deduced to be int &.

We can derive four rules for more information about the following code

    int i = 4;
    int arr [5] = {0};
    int * ptr = arr;
    struct S {double d;} s;
    void Overloaded (int);
    void Overloaded (char); // overloaded function
    int && RvalRef ();
    const bool Func (int);
 
    // Rule number one: to derive its type
    decltype (arr) var1; // int marker expression
 
    decltype (ptr) var2; // int * marker expression
 
    decltype (s.d) var3; // doubel member access expressions
 
    // Decltype (Overloaded) var4; // overloaded functions. Compilation errors.
 
    // Rule number two: the death value. Derivation for the type of rvalue references.
 
    decltype (RvalRef ()) var5 = 1;
 
    // Rule number three: the left value is derived for the type of reference.
 
    decltype ((i)) var6 = i; // int &
 
    decltype (true i:? i) var7 = i; // int & conditional expression returns a value left.
 
    decltype (++ i) var8 = i; // int & ++ i return i lvalue.
 
    decltype (arr [5]) var9 = i;. // int & [] operator returns an lvalue
 
    decltype (* ptr) var10 = i; // int & * Returns the value of the left
 
    decltype ( "hello") var11 = "hello"; // const char (&) [9] to the left of a string literal value, and is const lvalue.

 
    // Rule Four: None of the above, the derivation of the present type
 
    decltype (1) var12; // const int
 
    decltype (Func (1)) var13 = true; // const bool
 
    decltype (i ++) var14 = i; // int i ++ return the right value
It should be reminded that the string literals value is left and left is const value, instead of a string literal is a constant value of the right.
So many rules, we write the code for it will inevitably too difficult to remember, especially Rule number three. We can use the C ++ 11 standard library template class is_lvalue_reference added to determine whether the left expression value:

    cout << is_lvalue_reference :: value << endl;
Results 1 represents a value to the left, the right result for the non-0 value.
Similarly, there is_rvalue_reference this template class to determine whether decltype inference result for the right value.
     
         
         
         
  More:      
 
- The bulk batch multiple rows of data collect (Database)
- Ubuntu Froxlor Server Administration panel installation (Server)
- Linux Hard Disk Partition and file system management (Linux)
- shell script: the number of characters in the text to print no more than 6 words (Programming)
- Linux centralized log server rsyslog (Server)
- Linux tool curl and wget advanced use (Linux)
- Storm basic framework for analysis (Programming)
- Use Aptik Battery Monitor monitoring Ubuntu laptop battery usage (Linux)
- Python dictionary applications get built-in method (Programming)
- Linux kernel log --dmesg (Linux)
- Ubuntu 12.04 installation instructions under GAMIT10.40 (Linux)
- Why did not Oracle privileges can also log in with sysdba (Database)
- Oracle database with test data insertion speed (Database)
- Two network security scanning tools under ubuntu (Linux)
- RHEL7.0 configuration CD-ROM to the local YUM (Linux)
- NIC configuration parameters under Linux (Linux)
- Installation and configuration under Linux SVN server - Backup - Recovery - Scheduled Tasks (Server)
- Based on OpenSSL for HTTPS service configuration (Server)
- How to install Linux Kernel 4.4 on Ubuntu (Linux)
- CentOS 6 / Linux su: Unable to set user ID: Resource temporarily unavailable (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.