0

Code:

int CCalendarSettingsGooglePage::GetReminderIntervalAsMinutes(int iReminderUnitType, int iReminderInterval) noexcept
{
    int iMinutes = 24 * 60; // DEfault

    if (iReminderUnitType == ReminderUnitType::Weeks)
        iMinutes = iReminderInterval * 7 * 24 * 60;
    else if (iReminderUnitType == ReminderUnitType::Days)
        iMinutes = iReminderInterval * 24 * 60;
    else if (iReminderUnitType == ReminderUnitType::Hours)
        iMinutes = iReminderInterval * 60;
    else if (iReminderUnitType == ReminderUnitType::Minutes)
        iMinutes = iReminderInterval;

    return iMinutes;
}

Analysis warning:

C26497 You can attempt to make CCalendarSettingsGooglePage::GetReminderIntervalAsMinutes constexpr unless it contains any undefined behavior (f.4).

According to here (about constexpr functions it does state:

The following rules apply to constexpr functions in Visual Studio 2017 and later:

  • It may contain if and switch statements, and all looping statements including for, range-based for, while, and do-while.

But the original warning does say unless it contains any undefined behavior. How do we confirm if it has undefined behaviour? I do not think so, but did not want to blindly turn it into a constexpr function.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Signed integer overflow is [undefined in C++](https://stackoverflow.com/q/16188263/1889329). Depending on the input the calculations can overflow. I believe the consequences of turning this function into a `constexpr` function are, that the compiler will reject the program with an error if the function is evaluated at compile time and the arguments cause the function to exhibit undefined behavior. – IInspectable Nov 04 '21 at 09:34
  • @IInspectable Sounds like I should just suppress the warning then. – Andrew Truckle Nov 04 '21 at 13:47

0 Answers0