Home c++ Design DeclType (AUTO)

Design DeclType (AUTO)

Author

Date

Category

Why do you need decltype (auto) when you return from the function if you can write Auto ?


Answer 1, Authority 100%

Auto This is “perjury” (decayed) type of expression, i.e. Links are lost and copying:

string & amp; f ();
auto x = f (); // Type X - String, link lost, the string was copied
AUTO G () {Return F (); } // Returns String
// Analogue in C++ 11: Auto G () - & gt; Decay & lt; decltype (f ()) & gt; :: type;

Decltype (EXPR) is the actual type of expression, without drying.
Decltype (Auto) is a convenient syntax that allows not to write expression inside DeclType .

decltype (auto) y = f (); // Type Y - String & Amp;
// Analogue in C++ 11: DeclType (f ()) y = f ();
DeclType (AUTO) H () {Return F (); } // Returns String & Amp;
// Analogue in C++ 11: AUTO H () - & gt; decltype (f ());

In other words, DeclType (Auto) This is a convenient replacement DeclType (EXPR) ,
A AUTO This is a short syntax for Std :: Decay & LT; DeclType (AUTO) & gt; :: Type .


Answer 2, Authority 17%

Auto

in C++ 11 The keyword AUTO is deprived of its initial meaning as a storage class specifier and is now used to implement automatic type removal provided that a clear initializer is specified. The compiler sets the type of variable to the type of initialization:

auto maton = 112; // Maton gets type int
Auto Pt = & amp; Maton; // PT gets type int *
Double FM (Double, Int);
AUTO PF = FM; // PF Gets Type Double (*) (Double, Int)

AUTO keyword can also simplify template ads. For example, if іl is an object of type STD :: INITIALIZER_LIST & LT; Double & GT; , the following code

for (std :: initializer_list & lt; double & gt; :: iterator p = il.begin ();
      P! = il.end (); P ++)

can be replaced as:

for (auto p = il.begin (); p! = il.end (); p ++)

DeclType

The DeclType keyword creates a type variable that is specified by the expression. The operator below means “assigning the same type as x”, where x is an expression:

decltype (x) y;

Here is another pair of examples:

double x;
int n;
decltype (x * n) q; // q gets the same type as x * n, i.e. Double.
DeclType (& amp; x) PD; // PD gets the same type as & amp x, i.e. Double *

This is especially useful in template definitions when the type may not be defined up to the creation of a specific instance:

template & lt; typename t, TypeName U)
Void EF (T T, U U)
{
  DeclType (T * U) TU;
  ...
}

tail returned

C++ 11 appeared a new syntax for declaring functions in which the returned type is specified after the function name and parameter list, and not in front of them:

double f1 (double, int); // Traditional syntax
AUTO F2 (Double, Int) - & gt; double; // New Syntax, // Returned Type is Double

The new syntax may look less readable than traditional features of functions, however it makes it possible to use DeclType to specify the returned type of template functions:

template & lt; typename t, TypeName U)
AUTO EFF (T T, U U) - & GT; DeclType (T * U)
{
}

The problem illustrated here is that when the compiler reads the list of EF F parameters, T, and u are not in the scope, so any use of decltype must be after this list of parameters. The new syntax makes it possible.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions