selectany

Microsoft 전용

컴파일러는 선언 된 전역 데이터 항목 (개체 또는 변수) 선택 된 COMDAT (패키지 함수)으로 알려 줍니다.

__declspec( selectany ) declarator

설명

링크 시 다중 정의 COMDAT 발각 되 면 링커는 하나를 선택 하 고 나머지 무시 됩니다.경우 링커 옵션 /opt: ref (최적화)는 링커 출력에서 참조 하지 않는 데이터 항목을 모두 제거 하려면 COMDAT 제거 발생 합니다 선택 되어 있습니다.

생성자 및 선언에는 정적 메서드 또는 전역 함수를 할당에 대 한 참조를 만들 및 /opt: ref 제거 방지 하지 는지 않습니다.데이터에 다른 참조가 없는 경우 부작용에서 이러한 코드를 맡길 수 합니다.

동적 초기화, 전역 개체에 대 한 selectany 참조 되지 않은 개체의 초기화 코드를 함께 삭제 합니다.

일반적으로 EXE 또는 DLL 프로젝트에서 전역 데이터 항목을 한 번만 초기화할 수 있습니다.selectany같은 머리글 하나 이상의 소스 파일에 표시 되는 경우 헤더에 정의 된 전역 데이터를 초기화 하는 중에 사용할 수 있습니다.selectanyC와 C++ 컴파일러에서 사용 됩니다.

[!참고]

selectany실제 초기화를 외부에서 볼 수 있는 전역 데이터 항목에만 적용할 수 있습니다.

예제

사용 하는 방법을 보여 주는이 코드는 selectany 특성:

//Correct - x1 is initialized and externally visible 
__declspec(selectany) int x1=1;

//Incorrect - const is by default static in C++, so 
//x2 is not visible externally (This is OK in C, since
//const is not by default static in C)
const __declspec(selectany) int x2 =2;

//Correct - x3 is extern const, so externally visible
extern const __declspec(selectany) int x3=3;

//Correct - x4 is extern const, so it is externally visible
extern const int x4;
const __declspec(selectany) int x4=4;

//Incorrect - __declspec(selectany) is applied to the uninitialized
//declaration of x5
extern __declspec(selectany) int x5;

// OK: dynamic initialization of global object
class X {
public:
X(int i){i++;};
int i;
};

__declspec(selectany) X x(1);

사용 하는 방법을 보여 주는이 코드는 selectany 도 사용 하는 경우 데이터는 COMDAT 접기 하는 특성의 /OPT:ICF 링커 옵션입니다.데이터에 표시 되어야 합니다 참고 selectany 에 배치는 const 섹션 (읽기 전용).읽기 전용 섹션을 명시적으로 지정 해야 합니다.

// selectany2.cpp
// in the following lines, const marks the variables as read only
__declspec(selectany) extern const int ix = 5;
__declspec(selectany) extern const int jx = 5;
int main() {
   int ij;
   ij = ix + jx;
}

Microsoft 특정 끝

참고 항목

참조

__declspec

C + + 키워드