최적화 설정 (-O3
대 none)에 따라 출력이 다른 예제를 찾았지만 -std=c++11 -Wall -pedantic
옵션이 있더라도 GCC 4.8.2는 경고를 표시하지 않습니다.최적화 옵션에 따라 다른 동작
이 특별한 경우에 나는 header.h
의 주석 처리 된 줄을 "잊어 버린"것이 실수이며, -O3
과 함께 c<int>::get()
이 인라인 된 것으로 가정합니다.
그러나 이러한 종류의 실수 (컴파일러 또는 링커 옵션)로부터 자신을 보호 할 수있는 방법이 있습니까?
header.h가 :
#ifndef HEADER_H
#define HEADER_H
template<class T>
struct c
{
int get() { return 0; }
};
// template<> int c<int>::get();
#endif
imp.cpp :
#include "header.h"
template<>
int c<int>::get()
{
return 1;
}
MAIN.CPP :
#include <iostream>
#include "header.h"
int main()
{
c<int> i;
std::cout << i.get() << '\n'; // prints 0 with -O3, and 1 without
}
빌드 :
c++ -std=c++11 -pedantic -Wall -O3 -c imp.cpp
c++ -std=c++11 -pedantic -Wall -O3 -c main.cpp
c++ -std=c++11 -pedantic -Wall -O3 imp.o main.o
동작이 어떻게 다른가요? – molbdnilo