Should I use constexpr like this?

I have this rather simple function, I have some values ​​that need to be calculated, but only once, and the best time will be at compile time. These values ​​depend only on this function. Is this a good use of constexpr or should I just declare them static const?

ps I know that the difference in performance is so insignificant that it doesn’t matter, but I want to do this the “Correct” 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 ) );
}
+5
source share
1 answer

Your use is fine, if not a little detailed. In this context, constexprthey constmean the same thing. There will be either one (or even both).

Fwiw, std::chrono::hours::period::num 60*60 ( ++ 11 cred: -)).

:

void MainWindow::UpdateDateTimes()
{
    constexpr std::chrono::seconds cdtOffsetSecs = std::chrono::hours(-5);
    constexpr std::chrono::seconds edtOffsetSecs = std::chrono::hours(-4);
    constexpr std::chrono::seconds cetOffsetSecs = std::chrono::hours(2);

    QDateTime time( QDateTime::currentDateTimeUtc() );

    ui->mTimeLocal->setDateTime( time.toLocalTime() );

    ui->mTimeCDT->setDateTime( time.addSecs( cdtOffsetSecs.count() ) );
    ui->mTimeEDT->setDateTime( time.addSecs( edtOffsetSecs.count() ) );
    ui->mTimeCET->setDateTime( time.addSecs( cetOffsetSecs.count() ) );
}

static. static. , static " ".

, :

void f(int);

void UpdateDateTimes()
{
    constexpr std::chrono::seconds cdtOffsetSecs = std::chrono::hours(-5);
    constexpr std::chrono::seconds edtOffsetSecs = std::chrono::hours(-4);
    constexpr std::chrono::seconds cetOffsetSecs = std::chrono::hours(2);

    f(cdtOffsetSecs.count());
}

-O1 ( ) clang++ lib++, :

    .globl  __Z15UpdateDateTimesv
    .align  4, 0x90
__Z15UpdateDateTimesv:                  ## @_Z15UpdateDateTimesv
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp2:
    .cfi_def_cfa_offset 16
Ltmp3:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp4:
    .cfi_def_cfa_register %rbp
    movl    $-18000, %edi           ## imm = 0xFFFFFFFFFFFFB9B0
    popq    %rbp
    jmp __Z1fi                  ## TAILCALL
    .cfi_endproc

:

void UpdateDateTimes2()
{
    f(-18000);
}

:

    .globl  __Z16UpdateDateTimes2v
    .align  4, 0x90
__Z16UpdateDateTimes2v:                 ## @_Z16UpdateDateTimes2v
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp7:
    .cfi_def_cfa_offset 16
Ltmp8:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp9:
    .cfi_def_cfa_register %rbp
    movl    $-18000, %edi           ## imm = 0xFFFFFFFFFFFFB9B0
    popq    %rbp
    jmp __Z1fi                  ## TAILCALL
    .cfi_endproc

imho , - .: -)

+7

All Articles