I have this pretty simple function, I have some values that need to be calculated but only once and the best time would be at compile time. These values only matter within this function. Is this a good use of constexpr or should I just declare them static const?
ps I know that the performance difference is so little to not matter, but I want to do it the "Right" c++11 way.
void MainWindow::UpdateDateTimes()
{
// for some dumb reason DateTime only has add seconds method
// so we have to calculate the seconds per hour and the number of hours
// we do this with static constant values so that the calculations
// only happen once.
static constexpr const int secsPerHour = 60 * 60;
static constexpr const int cdtOffsetHours = -5;
static constexpr const int edtOffsetHours = -4;
static constexpr const int cetOffsetHours = 2;
static constexpr const int cdtOffsetSecs = secsPerHour * cdtOffsetHours;
static constexpr const int edtOffsetSecs = secsPerHour * edtOffsetHours;
static constexpr const int cetOffsetSecs = secsPerHour * cetOffsetHours;
QDateTime time( QDateTime::currentDateTimeUtc() );
ui->mTimeLocal->setDateTime( time.toLocalTime() );
ui->mTimeCDT->setDateTime( time.addSecs( cdtOffsetSecs ) );
ui->mTimeEDT->setDateTime( time.addSecs( edtOffsetSecs ) );
ui->mTimeCET->setDateTime( time.addSecs( cetOffsetSecs ) );
}
constexprimpliesconst. - Xeoconst intis sufficient for every, butconstexpr intmight be more explicit. Please don't mix the two. - ipcstaticthey are not created for every instance - balki