The following code builds, compiles and runs (C++, mingw) seemingly without any problems. However, am I guaranteed that objects constructed with initializer-lists inside a function's parameter list, lives through the scope of that function, even though the function takes the argument by reference?
If not, is it true that when creating an object using its initializer-list in a function's parameter list (which takes the argument by reference) may be dangerous because it will immediately be destructed: In this case, the function doesn't have a copy, but a reference to memory which may, or may not be reallocated by another process?
struct S { S() : a(0), b(0) {} S(int a, int b) : a(a), b(b) {} int a; int b; }; void foo(const S& s) { std::cout << "s.a = " << s.a << std::endl; std::cout << "s.b = " << s.b << std::endl; } int main() { foo({4,5}); // <-- What is the scope of the struct initialized here? return 0; }
A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters.
Any variable used inside a function is by default limited to the local function scope.
Scope of Variables in C++ In general, the scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with.
Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. This means that local variables can be accessed only inside the function in which they are declared, whereas global variables can be accessed throughout the program body by all functions.
Scope of function parameters. Local variables have their scope to the function only in which they are declared, while global variables have scope to the program and they can be accessed by any function in the program. Look at following function: Here, x and y are the function parameters and sum is the local variable.
Parameters to a function call have local scope unless they are passed by reference ( then also the change is visible to calling function only which is not true definition of global scope). So the variables defined inside the functions or as parameters to the function have local scope.
In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes: Now let’s understand each of the scope at a greater detail:
There are mainly two types of variable scopes: Now let’s understand each of the scope at a greater detail: Variables defined within a function or block are said to be local to those functions.
According to cppreference [lifetime]:
All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation. This is true even if that evaluation ends in throwing an exception.
That means that the temporary object will be destroyed after the function has returned, so it's perfectly safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With