최대 1 분 소요

개요

  • inline function
    • C++98
    • 컴파일러에게 함수 호출 대신 호출 부분을 함수 내용으로 대체할 것을 요청
    • 컴파일러에 따라 inline이 없어도 상황에 따라 inline으로 동작하기도 함
  • inline variable
    • C++17
    • non-const static member를 선언과 동시에 초기화 가능


예제

  • 코드
    #include <compare>
    #include <iostream>

    using namespace std;

    struct Test {
    		inline static int i = 1;
    };

    inline void func() { cout << "func()" << endl; }

    int main() {
    	cout << Test::i << endl;
    	++Test::i;

    	Test t;
    	cout << t.i << endl;

    	func();

    	return 0;
    }
  • 실행 결과
    1
    2
    func()