Compartilhar via


Aviso do compilador (nível 1) C4172

endereço de retorno da variável local ou temporário: optional_context

Observações

Uma função retorna o endereço de uma variável local ou objeto temporário. Variáveis locais e objetos temporários são destruídos quando uma função retorna, portanto, o endereço retornado não é válido.

Reprojete a função para que ela não retorne o endereço de um objeto local.

Exemplo

O exemplo a seguir gera 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
}