Reading assignment - auto variables

  1. It is a variable that follows the template argument rules which means that the type is deduced from the initializer
  2. It is convenient when you don’t know the type of a variable
  3. When we use lambda functions
  4. Compile time according to stackoverflow
1 Like

An auto variable is a variable that can automatically detect the data type of the variable. Auto variables can be convenient to know what data type the variable is because of C++ want for strict variable types. Auto variables can be used when you want to stay flexible in the type of data you insert.

1 Like
  1. An auto variable is a variable that has it’s type cast at initialization
  2. It makes it faster to type, easier to read
  3. When it’s easier to type auto instead of determining the type of the initialization expression
  4. At compile-time
1 Like
  1. variable type that will dynamically be determined at run-time
  2. it makes it more flexible
  3. when you con’t know the exact outcome yet
  4. run-time
1 Like

1. What is an auto variable?
its a variable that will automatically determinate his type by its initializer. EG: auto a = 1 (int) or auto a = 1.5 (float). the initializer “1” or “1.5” or something else, will determinate the data type that variable will have.

2. Why is it convenient to use?
Mostly on cases that u cant know wich data type will a function returns, this variable will save you time since it will determinate it for you when you compile the code.

3. When should you use it?
Data type returned by functions or other source is unknown.

4. When will the type of an auto variable be determined (compile-time or run-time?)
compile-time.

1 Like
  • What is an auto variable?
    An auto variable is the type of the variable that is being declared will be automatically deduced from its initializer.

  • Why is it convenient to use?
    You don’t need to declare all the types in advance

  • When should you use it?
    When you are unsure of the type of the variable that will be used.

  • When will the type of an auto variable be determined (compile-time or run-time?)
    Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto

1 Like
  1. What is an auto variable?
    An auto variable is one which does not have a type declaration and its type is inferred by its initialiser. i.e.
    unique_ptr w = make_unique();
    becomes…
    auto w = make_unique();

  2. Why is it convenient to use?
    It is convenient to use;
    a. when you have types that have long or difficult to spell names.
    b. when variables are difficult to read because they are hidden amongst clutter. The variable is moved to the left of the “=”.
    c, where variable types are unknown, as in the case where the same code is used by different template type instances.
    d. where the type is not nameable, such as a lambda function.
    e. when iterating through containers and don’t need to know the type definitions used within those containers. i.e. using vectors or lists interchangeably. Note, that types must be convertible.

  3. When should you use it?
    You should use auto when the variable type is dependent on other code. A philosophy behind this is “Write code for interfaces, not implementations” as it creates more generic and less interdependent code. It is also good practice as it forces variable initialisation.

  4. When will the type of an auto variable be determined (compile-time or run-time?)
    It is determined at compile-time. Using auto does not introduce weak typing. The compiler must be able to deduce the type at compile time so C++ remains a strongly typed language when using auto.

2 Likes
  1. Auto variable is a variable that is declared automatically. We do not need to declare it as an int, float, etc.

  2. It is useful in functions when we don`t yet know the value.

  3. Should be used when we don`t know what type of variable our input will be.

  4. Compile time

2 Likes

A variable thats type is beeing assigned by the compiler

I guess if it is not clear which type the variable should be assigned to. Also it seems that it makes things easier. In for example JS, you dont have to think about whether your varible is an integer or string or whatever, you just say “var a = 2;” and now with the auto variable the compiler does the “work” for you

auto is commonly used for unnamed types such as the types of lambda expressions (don’t know what this means tho)

compile-time

2 Likes

What is an auto variable? In the type specifier of a variable: auto x = expr;. The type is deduced from the initializer.
If the placeholder type specifier is auto or type-constraint auto (since C++20), the variable type is deduced from the initializer using the rules for template argument deduction from a function call.
Why is it convenient to use? Because variable types are deduced from the initializer.
When should you use it? When the placeholder type specifier is used to declare multiple variables, the deduced types must match. For example, the declaration auto i = 0, d = 0.0; is ill-formed, while the declaration auto i = 0, *p = &i; is well-formed and the auto is deduced as int.
When will the type of an auto variable be determined (compile-time or run-time?) Compile-time.

1 Like

1. What is an auto variable?

Placeholder Type Specifiers

For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer.

2. Why is it convenient to use?

Allows leaving the type deduction to the compiler itself. Less time is spent having to write out things the compiler already knows.

3. When should you use it?

Types can become vastly more convoluted and complex than C# types due to metaprogramming and other factors. AUTO is faster to write and read and more flexible/maintainable than an explicit type.

4. When will the type of an auto variable be determined (compile-time or run-time?)

Compile-time

2 Likes

What is an auto variable?
The auto keyword is simply asking the compiler to deduce the type of the variable from the initialization.

Why is it convenient to use?
C++ is a strong data typed language and allows to create personalized User defined datatype ( like struct class etc … ).
Figuring out which datatype can be “cumbersome”.

When should you use it?
When I don’t know what types to expect.
Many iterators to access STL or nultiboost Containers have datatypes not easy to figure out.
It is very useful in that case.

When will the type of an auto variable be determined (compile-time or run-time?)
run-time

1 Like

3.YES - Using auto is so helpful when it comes to “iterators” within STL or Boost::multi_index containers where the datatype can be sometimes Cumbersome to figure out !!
Thanks .JJ

  1. It is a variable whose type is specified automatically and deduced from the initializer.

  2. Because we let C++ determine what is the correct variable type. Also, I think that in some cases, when we have a rather long and complicated type (such as vector code), it can help make the code cleaner. For instance, auto might be cleaner than vector<int>::iterator .

  3. When the type is complex, as mentioned above, or when it is yet unknown for some reason.

  4. Compile time. Runtime type information is stripped during compilation

1 Like

Well said, in a concise way :+1:

1 Like
  1. It is a placeholder that allows to identify the type of a variable basing on its initializer.
  2. It is especially useful to build functions that have to operate on variables which type is not known.
  3. One should use an auto variable to increase readability and to keep the function flexible despite the variable type.
  4. At compile time.
1 Like
1. What is an auto variable?"

A variable who’s type is inferred from how it is initialised.

2. Why is it convenient to use?"

Less typing for long type names like template types

3. When should you use it?"
  • The article says it’s often used with lambdas
  • Any time when writing or figuring out the type is annoying
4. When will the type of an auto variable be determined (compile-time or run-time?)"

Compile time. C++ does everything at compile time as far as I understand.

1 Like
  1. What is an auto variable?

It is a variable which has its type deduced from the expression used to initialize it.

  1. Why is it convenient to use?

It is convenient especially when we are assign a function to a variable, without knowing the return value.

  1. When should you use it?

When you are unsure of the type of the variable that will be used.

  1. When will the type of an auto variable be determined (compile-time or run-time?)

Compile-time

1 Like
  1. What is an auto variable?
    A variable which it’s type will be automatically deduced by the initializer expression.

  2. Why is it convenient to use?
    For a number of reasons. Among others: saves time when writing (some variable types may become quite large), can make code more clear and compact, it forces us to initialize variables, etc.

  3. When should you use it?
    When code readability improves.

  4. When will the type of an auto variable be determined (compile-time or run-time?)
    The type is determined by the compiler (at compile-time).

1 Like
  1. The data type is automagically determined.
  2. less strict and when you don’t know the exact type
  3. when you’re not sure, lambdas, combining values
  4. compile time
1 Like