Reading assignment - auto variables

Welcome to the discussion about the reading assignment about auto variables.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

  1. What is an auto variable?
  2. Why is it convenient to use?
  3. When should you use it?
  4. When will the type of an auto variable be determined (compile-time or run-time?)
2 Likes
  1. The data type of the variable will be detected automatically
  2. It can be convenient if it is not known (yet) what data will be in the variable(s) yet. As C++ is very strict in variable types, I guess it makes programming easier and more flexible and less strict (as for example programming in PHP)
  3. When you want to stay flexible in which data you insert, or want to be flexible about if f.e. data like ‘2’ is a number or or a string.
  4. On run-time, as it can change / be updated during run-time
1 Like
  1. Using auto can achieve, assigning data type dynamically based on declaration of its initializer.
  2. It is convenient especially when we are assign a function to a variable, without knowing the return value.
  3. We can use this when we declare variables in block scope, namespace scope and initialization statement of for loop.
  4. Compile-time
1 Like

1.auto variable in C++ has its type determined by a C++ compiler at compile type without a programmer having to specify the variable type explicitly, e.g. int, float, double etc.

  1. auto variable is convenient to use as it spares the programmer the necessity to write a long type specification as with iterator for example, where explicit type specification might be quite long
  2. One should use auto variables with lambda functions, since lambda functions do not have a nameable type. Another scenario for usage is with
    function and class templates where instead of determining a return type based on what template parameter used, a programmer will let C++ compiler to deduce the type automatically. And yet another usage scenario is with iterators where using auto shortens the amount of typing a programmer should perform.
  3. The type of auto variable is deduced at compile tyme by c++ compiler
3 Likes

1. What is an auto variable? An auto variable is a variable that has its type automatically deduced by its initializer.

2. Why is it convenient to use? They are particularly convenient when working with lengthy data type specifiers, where the initialisation expression for the variable is clear. Rather than explicitly typing out the whole type specifier, you can simply use the auto keyword instead.

3. When should you use it? You should use auto variables in places where it improves the readability of your code, such as replacing lengthy data types when it’s immediately clear what the variable type is. They should also be used for storing lambda expressions when you know you’ll be reusing the same lambda expression.

4. When will the type of an auto variable be determined (compile-time or run-time?) For auto variables, it occurs during compile-time.

2 Likes
  1. the auto keyword enables the user to not having to specify the exact data type of the variable. The compiler will do that instead (following the rules of templates)
  2. when we don’t know the data type of our variable yet, the use of auto variables is convenient.
  3. it should be used to store often reused lambda expressions and to replace long data types to improve redeablility / complexity of our code
  4. at compile time
1 Like

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

2. It is convenient to use if you don’t know the type of a variable, for example the return type of a function. It can also increase the readability of the code and save the programmer some typing.

3. auto should be used as the type specifier when declaring variables in block scope, in namespace scope, in initialization statements of for loops, as lambda expressions etc.

4. run-time

1 Like
What is an auto variable?

A variable that has its type determined by the compiler.

Why is it convenient to use?

to be continued…

When should you use it?

to be continued…

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

I’m assuming compile time as it is the compiler that does the determining.

2 Likes

What is an auto variable?
type is determined during run-time
Why is it convenient to use?
Gives type flexibility to functions.
When should you use it?
When you don’t know what types to expect to be passed to a function.
When will the type of an auto variable be determined (compile-time or run-time?)
Run-time (so that user input can be considered in the typing, or functions can be used for multiple types).

3 Likes
  1. An auto variable is the type of the variable that is being declared will be automatically deduced from its initializer.

  2. If you are unsure as to what type your variable takes

  3. When you want to remain open ended while declaring a variable

  4. compile-time

2 Likes

What is an auto variable?
A variable that will get it’s type when it is initialized

Why is it convenient to use?
If you’re unsure of what type you should use or when you’re combining multiple variable types into a single variable or function call

When should you use it?
Whenever you’re not sure what the variable’s value will end up being - useful for lambdas and combining values.

When will the type of an auto variable be determined (compile-time or run-time?)
It depends on when the variable is instantiated it could be either one.

2 Likes

1. What is an auto variable?
It is a variable whose type will be automatically deduced from its initializer
2. Why is it convenient to use?
Allows more flexibility to code
3. When should you use it?
When you are unsure of the type of the variable that will be used.
4. When will the type of an auto variable be determined (compile-time or run-time?)
The type of the auto variable will be determined ad compile-time

2 Likes

1. What is an auto variable?
Variable declared using ‘auto’. It must be initialised at same time too. Its type is deduced by compiler.

2. Why is it convenient to use?
Simplicity, avoid writing lengthy types e.g. in a for loop which iterates a vector to type strings etc.

3. When should you use it?
When specific type is not required e.g. Number can be an int, float, long etc. Also as mentioned above when type is very lengthy.

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

2 Likes

1. What is an auto variable?
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type or will be deduced from its return statements (since C++14) for non-type template parameters, specifies that the type will be deduced from the argument

2. Why is it convenient to use?
When we don’t know the data types of variables we will be working with or to save time writing long type specifications.

3. When should you use it?
When you need or want to be open ended with your variable types.

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

2 Likes
  1. An auto variable is a variable whose type will be assigned automatically.
  2. When we don’t know the data types of variables we will be working with or to save time writing long type specifications.
  3. When specific type is not required e.g. Number can be an int, float, long etc. Also as mentioned above when type is very lengthy.
  4. Compile Time
2 Likes

image

  1. What is an auto variable?
  2. The auto type is a specifier that automatically deducts the type of variable from it's initializer.

  3. Why is it convenient to use?
  4. Auto variables are convinient to use because it will automatically assign the type. This allows the user not to spend brain power on knowing or guessing the type specially since C++ is very strict with it's compiler.

  5. When should you use it?
  6. When we do not know the variable type we should use the auto specifier. Examples are defining complex types, when using nested containers from a standard template library, when auto forcing initialization of variables and finally when storing Lambda closures.

  7. When will the type of an auto variable be determined (compile-time or run-time?)
  8. In C++, runtype information is stripped during compilation and therefor each data type needs to be explicitly declared at compile time.
4 Likes
  1. Auto will find the type of the variable being declared on its own.
  2. It is convenient to use because you will not have to determine the variable type at the moment of initialization.
  3. You should use it when the return data type is unknown at the moment.
  4. The type is determined at compile-time.
1 Like

What is an auto variable?
type specifier

Why is it convenient to use?
The compiler determines the type that will replace the keyword auto

When should you use it?
When declaring variables in block scope, in namespace scope, in initialization statements of for loops, etc.

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

1 Like

What is an auto variable?

  • a variable that is created without a specific type

Why is it convenient to use?

  • you don’t need to declare all the types in advance

When should you use it?

  • When it can make your life simpler, and code readability

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?
    the type of variable will be automatically deduced from its initilizer

  2. Why is it convenient to use?
    we don’t need to know the type from the begining

  3. When should you use it?
    when you don’t know the type yet or if you want to be flexible

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

1 Like