Ad Space — Top Banner

E2357

C++ Builder Programming Language

Severity: Minor

What Does This Error Mean?

E2357 means you tried to bind a non-const reference to an rvalue — a temporary value that does not have a persistent memory address. In C++, a non-const reference must be bound to an lvalue (a named variable with a stable address). The fix is to either store the temporary in a named variable first, or change the reference parameter to const.

Affected Models

  • C++ Builder 10.x (Alexandria)
  • C++ Builder 11 (Sydney)
  • C++ Builder 12 (Athens)
  • RAD Studio 11 and 12

Common Causes

  • Passing a literal or expression directly to a function that takes a non-const reference parameter
  • Passing the result of a function call to a non-const reference parameter
  • Initializing a non-const reference variable with a temporary object
  • Passing a value of a different (but convertible) type to a non-const reference — the implicit conversion creates a temporary

How to Fix It

  1. Store the temporary in a named variable first, then pass the variable to the reference parameter. Named variables are lvalues and can bind to non-const references.

    This is the cleanest fix when you genuinely need a mutable reference. It also makes the code easier to read since the intermediate value has a name.

  2. If the function does not modify the parameter, change the parameter type from a non-const reference to a const reference. Most input-only parameters should be const references anyway.

    A const reference can bind to both lvalues and rvalues (temporaries). Changing to const reference is the correct fix when the function does not need to modify the passed value.

  3. In C++11 and later, if the function needs to take ownership of a temporary, use an rvalue reference parameter (double ampersand) or pass by value instead.

    Rvalue references are for move semantics — transferring ownership of resources. For most cases in VCL/C++ Builder code, switching to const reference is the simplest correct fix.

Frequently Asked Questions

Why does passing an integer literal to a reference parameter cause E2357?

A literal like 42 is a temporary value — it has no address and cannot be modified. A non-const reference is a promise that you might modify the value and the caller will see the change. Binding a reference to a temporary would break that promise, so the compiler forbids it. Use const int& for input-only integer parameters.