Bemærk
Adgang til denne side kræver godkendelse. Du kan prøve at logge på eller ændre mapper.
Adgang til denne side kræver godkendelse. Du kan prøve at ændre mapper.
The function 'function' is constexpr, mark variable 'variable' constexpr if compile-time evaluation is desired (con.5)
This rule helps to enforce Con.5: Use constexpr for values that can be computed at compile time in the C++ Core Guidelines.
Remarks
The warning is triggered by assigning the result of a constexpr function to any non-constexpr variable whose value doesn't change after the initial assignment.
Code analysis name: USE_CONSTEXPR_FOR_FUNCTIONCALL
Example
This sample code shows where C26498 may appear:
constexpr int getMyValue()
{
return 1;
}
void foo()
{
constexpr int val0 = getMyValue(); // no C26498
const int val1 = getMyValue(); // C26498, C26814
int val2 = getMyValue(); // C26498, value is never modified
int val3 = getMyValue(); // no C26498, val3 is assigned to below.
val3 = val3 * val2;
}
To fix the issues, mark val1 and val2 constexpr:
constexpr int getMyValue()
{
return 1;
}
void foo()
{
constexpr int val0 = getMyValue(); // OK
constexpr int val1 = getMyValue(); // OK
constexpr int val2 = getMyValue(); // OK
int val3 = getMyValue(); // OK
val3 = val3 * val2;
}
See also
C26497
C26814
Con.5: Use constexpr for values that can be computed at compile time