次の方法で共有


コンパイラの警告 (レベル 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
}