Sign in with Google to keep private notes

Lifetimes

Not all types of variables are destroyed when they go out of scope.

Best practice: Define your local variables as close to their first use as reasonable.

Defining your variables at the start of your function is considered suboptimal

Scopes can be created whenever you really want them to be created

int main() 
{
    int x { 5 }; 

    { // nested block
        int y { 7 }; // y enters scope and is created here
    } // y goes out of scope and is destroyed here



    return 0;
}

Local variables have no linkage

Scope and linkage are quite similar but they do refer to distinctly different concepts

I guess the whole concept of the scope blocks can give this compiler the ability to decide how much space to allocate to each call stack.

Name hiding or shadowing is the thing that happens when you declare an int x in an outer block and also declare another int x in an inner block. Very boring stuff.

Something I never really processed but subconsciously knew was the role of declarations within inner blocks allowing changes which occur within blocks to not take scope outside of blocks. I did in python and I can do it in C++

I mean the humble for loop does it too:

int i = 100;

for (int i = 0; i < 5; i++) { doSomething(); } 

global variables can also get shadowed by the way

to note is that making function calls shirks off any shadowing effect occuring, and the value returns to the global version

which is, weird, to me. so thats a difference between setting up a different block and setting up a function call

although, i do now get a bit curious how that differs inline. hm.

#include <iostream>

using namespace std;


int i = 5;

void foo() {
    cout << i << endl;
}

inline void bar() {
    cout << i << endl;
}

int main() {
    int i = 8;
    cout << i << endl;
    foo();
    bar();
}

although, using ::i successfully allows you to get access to the global i variable. So, that's pretty cool.

this is calle dthe scope resolution operator. You may have seen it tacked onto the back of std::cout. Which is kind of cool i guess.

namespaces are especially important for functions, because C++ generally does not allow defining functions inside of other functions

although, again, this is not recommended by learncpp

this will still print 8 5 5 by the way.

Inline doesn't mean "verbatim paste code here", it will still respect the "hierarchy of scope", i think.

The purpose of inlining evolved over time to being about being able to redefine the same function across multiple translation units without violating one definition rule. Although then it becomes your responsibility to ensure its defined the same way throughout to prevent undefined behaviour...at which point I'd continue questioning why anyone would use this feature.

the following is real

__attribute__((always_inline)) inline void bar() {
    cout << i << endl;
}

namespace operator

executable statements are not allowed in namespaces. So this is why you can't go int x; and then x = 5;. As a matter of fact you shouldn't be declaring non-const variables in global namespace anyway

For the sake of your code remaining maintainable you probably also should not be using the using directive. Because one day std might define a function with the same name as the one you just wrote, and then thats going to lead to naming conflicts

forward definitions have to be done in the same namespace

Articles to read next