다음을 통해 공유


컴파일러 경고(수준 1) C4172

지역 변수 또는 임시 주소 반환: optional_context

비고

함수는 지역 변수 또는 임시 개체의 주소를 반환합니다. 함수가 반환될 때 지역 변수 및 임시 개체가 제거되므로 반환된 주소가 유효하지 않습니다.

로컬 개체의 주소를 반환하지 않도록 함수를 다시 디자인합니다.

예시

다음 예제에서는 C4172를 생성합니다.

// C4172.cpp
// compile with: /c /W1

const int* func1()
{
    int i = 42;
    return &i;   // C4172
}

float f = 1.f;

const double& func2()
// Try one of the following lines instead:
// const float& func2()
// const auto& func2()
{
    // The problem is that a temporary is created to convert f to a double.
    // C4172 in this case refers to returning the address of a temporary.
    return f;   // C4172
}