Storage classes w/out inline und ihre genaue Bedeutung / Einsatzzwecke



  • Ich muss nach einigen Jahren Abstinenz wieder etwas C++ programmieren und will mir gerade die storage classes wieder einverleiben, bin aber teilweise noch etwas am Holzweg. Außerdem ist mir noch nicht ganz klar was das neue "inline" jeweils genau modifiziert. Vielleicht könnt ihr mir auf die Sprünge helfen und mein Zusammengeschriebens ergänzen bzw. dort wo mir Dinge unklar sind aushelfen:

    // * Declared & defined here
    // * Must be a compile-time constant
    // * May be completely inlined by the compiler at compile-time
    // * Has no address
    constexpr int constexprInt = 5;
    
    // * Declared here, defined somewhere else
    // * Does not have to be a compile-time constant
    // * Reasons: to be able to update the value by updating just the library, to use private methods to initialize it, etc.
    // * Each compile unit references the same external instance (i.e., every cpp that includes this header sees the same address)
    const extern int constExternInt;
    
    // * Declare & defined here
    // * Does not have to be a compile-time constant
    // * Each compile unit gets its own instance (i.e., every cpp that includes this header sees a different address)
    // * Reasons to use this approach over extern?
    const static int constStaticInt = 5;
    
    // * Equivalent to constStaticInt?
    const int constInt = 5;
    
    // * I'm completely unsure what inline gives me over constexprInt? Maybe constexpr is always inline?
    constexpr inline int constexprInlineInt = 5;
    
    // * I'm completely unsure what inline gives me over constExternInt? Maybe extern is always inline? Sounds wrong...
    const extern inline int constExternInlineInt;
    
    // * In contrast to constStaticInt, everybody sees the same address again? Soooo, it's kinda like extern, with the only difference that we can put the initialization in here?
    const static inline int constStaticInlineInt = 5;
    
    // * Equivalent to constStaticInlineInt
    const inline int constInlineInt = 5;
    
    // * Declared here, defined somewhere else
    // * Use this by default if putting an object in a header?
    extern int externInt;
    
    // * Declared here, defined here
    // * Each compile unit gets its own instance (which, in contrast to constStaticInt, makes sense to me, because the value can be different)
    static int staticInt = 5;
    
    // * Equivalent to staticInt?
    //int Int;
    
    // *  I'm completely unsure what inline gives me over externInt? Maybe extern is always inline? Sounds wrong...
    extern inline int externInlineInt;
    
    // * In contrast to staticInt, everybody sees the same address again? Soooo, it's kinda like extern, with the only difference that we can put the initialization in here?
    static inline int staticInlineInt = 5;
    
    // * Equivalent to staticInlineInt?
    inline int inlineInt = 5;
    

    Freue mich über jeden guten Input 🙂

    MfG SideWinder



  • Da sich niemand - aus verständlichem Grund - traut etwas dazu zu sagen ... bis auf extern und static sind da keine storage class specifiers.

    @SideWinder sagte in Storage classes w/out inline und ihre genaue Bedeutung / Einsatzzwecke:

    // * Declared & defined here
    // * Must be a compile-time constant
    // * May be completely inlined by the compiler at compile-time
    // * Has no address
    constexpr int constexprInt = 5;
    

    Der Standart sagt dazu daß der initializer eine constant expression sein soll.

    @SideWinder sagte in Storage classes w/out inline und ihre genaue Bedeutung / Einsatzzwecke:

    // * Equivalent to constStaticInt?
    const int constInt = 5;
    

    Ja, const at namespace level impliziert internal linkage. static wäre also doppelt gemoppelt.



  • Wieso traut sich niemand etwas sagen? Einfach drüberfahren bitte 🙂

    Ja, extern/static/<keine angabe> kommt vor. Und mir ist nicht überall genau klar obs einen Unterschied macht ob die Sache const ist oder nicht - für die storage class wahrscheinlich nicht, aber ich könnte mir vorstellen, dass der Einsatzzweck für "const <storageclass> x" und "<storageclass> x" oftmals ein anderer ist.

    Am liebsten stelle ich mir am Ende so eine Art Entscheidungsbaum vor, der mich guidet die richtige storage class auszuwählen. Ich glaub, wenn ich alle Infos zusammen habe, schreib ich dazu einen Blog-Artikel. Aber dafür fehlts mir noch an Wissen derzeit.

    MfG SideWinder



  • @Swordfish sagte in Storage classes w/out inline und ihre genaue Bedeutung / Einsatzzwecke:

    Ja, const at namespace level impliziert internal linkage. static wäre also doppelt gemoppelt.

    non-const auf namespace-level impliziert aber auch internal linkage? Oder nicht?

    MfG SideWinder



  • @SideWinder Im global namespace nicht.

    Vielleicht ist der Artikel für Dich interessant: C++ Reference Material - Storage Classes and Linkage



  • @SideWinder sagte in Storage classes w/out inline und ihre genaue Bedeutung / Einsatzzwecke:

    non-const auf namespace-level impliziert aber auch internal linkage? Oder nicht?

    Ich glaub mal nicht, nein.



  • basic.link/3

    3. A name having namespace scope has internal linkage if it is the name of

    (3.1) a variable, variable template, function, or function template that is explicitly declared static; or

    (3.2) a non-template variable of non-volatile const-qualified type, unless

    (3.2.1) it is explicitly declared extern, or

    (3.2.2) it is inline or exported, or

    (3.2.3) it was previously declared and the prior declaration did not have internal linkage; or

    (3.3) a data member of an anonymous union.

    [Note 1: An instantiated variable template that has const-qualified type can have external or module linkage, even if not declared extern. — end note]


Anmelden zum Antworten