Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

does making static inline inside namespace is redundant

is there any significance of making a variable and function inline and static inside a namespace. For example consider following 2 files:

consider first header file

// header1.h
#include <string>
#include <iostream>
namespace First
{
    static inline std::string s {"hi"};

    static inline void print(std::string str)
    {
        std::cout << str << std::end;
    }
}

and consider following second header file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// header2.h
#include <string>
#include <iostream>
namespace Second
{
    std::string s {"hi"};

    void print(std::string str)
    {
        std::cout << str << std::end;
    }
}

Does compiler see two codes differently or they introduce similar behavior ?

>Solution :

Do not namespace variables and functions have internal linkage, like unnamed namespace ?

No, your assumption that namespace(named namespaces) variables and functions by default have internal linkage is incorrect.

Does compiler sees two codes differently or they introduce similar behavior ?

The one with thestatic keyword have internal linkage while the other(in header2.h) have external linkage.

That is, the variable s in header1.h and header2.h are distinct from each other. Similarly, the functions are also different from each other.

header1.h

namespace First
{
    static inline std::string s {"hi"};       //s has internal linkage

    static inline void print(std::string str) //print has internal linkage
    {
        std::cout << str << std::end;
    }
}

header2.h

namespace Second
{
    std::string s {"hi"};        //s has external linkage

    void print(std::string str)  //print has external linkage
    {
        std::cout << str << std::end;
    }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading