6.13 Unnamed and inline namespaces

주홍영·2022년 3월 12일
0

Learncpp.com

목록 보기
72/199

https://www.learncpp.com/cpp-tutorial/unnamed-and-inline-namespaces/

Unnamed namespace

말그대로 namespace가 이름없이 정의된 경우이다
매우 쓸모없어 보이지만
internal linkage와 같이 사용할 수 있다
이는 모든 function을 static function으로 선언하는 것과 같은 효과를 가진다

inline namespace

#include <iostream>

inline namespace v1 // declare an inline namespace named v1
{
    void doSomething()
    {
        std::cout << "v1\n";
    }
}

namespace v2 // declare a normal namespace named v2
{
    void doSomething()
    {
        std::cout << "v2\n";
    }
}

int main()
{
    v1::doSomething(); // calls the v1 version of doSomething()
    v2::doSomething(); // calls the v2 version of doSomething()

    doSomething(); // calls the inline version of doSomething() (which is v1)

    return 0;
}

위와 같은 경우
출력은

v1
v2
v1

이다 그냥 doSomethin();의 경우 inline namespace가 있어서 v1이 실행된다
위와 같은 활용법이 있으나 제한적인 활용법으로 자세히 다루지는 않겠다

profile
청룡동거주민

0개의 댓글