How to tell if expression is evaluated at compile time or runtime?

DEKKER

I have a rather big Map object and I want to have a separate list that has the keys sorted. This will be used in many other source files of my poject.

The question is about how do I know when a decalaration/definition is a compile time job. Where should I look to find if this is the case? I mean how to tell?

In the following example, is the list in the source file a compile time job or it happens at runtime?

Also, is there a way that I make the sorting operation at compile time?

// global.h    
extern QMap<int, QString> G_MAP;
extern QList<int> G_MAP_SKEYS_SORTED; 

// global.cpp
QMap<int, QString> G_MAP = { /* some hand filled (static) data */ };
QList<int> G_MAP_SKEYS_SORTED = G_MAP.keys();

// main.cpp
int mian() {
  // Somewhere I do the sort
  std::sort(G_ListRegistersSorted.begin(), G_ListRegistersSorted.end());
}
J. Antonio Perez

An expression is evaluated at compiletime if the result is assigned to a constexpr variable, used in a static_assert or noexcept statement, or used as a template parameter. This is called a constexpr context.

For example:

// Function which can calculate the fibbonacci sequence at compiletime
constexpr int fib(int n) {
    if(n == 0 || n == 1) return n;
    return fib(n - 1) + fib(n - 2); 
}

int main() {
    // This one is calculated at compiletime
    constexpr int fib10_at_compiletime = fib(10); 

    // This one is calculated at runtime 
    // (unless the compiler was really aggressive when doing optimizations)
    int fib10_at_runtime = fib(10);    
}

In order to call a function or something at compiletime, it needs to be marked constexpr.

What can you do at compiletime?

C++11:

  • Declare variables (but not modify them)
  • Call other constexpr functions
  • Call constexpr constructors (and default ones)
  • Use carrays and std::array
  • Use static_asserts and stuff
  • typedef and using declarations

C++14 additions:

  • You can also use lambdas now
  • You can modify variables inside a constexpr function
  • you can have constexpr member functions that change member variables
  • you can pass references (the non-const kind) to constexpr functions

C++20 additions: (C++20 is coming out in 2020)

  • You can allocate memory now
  • You can call virtual functions now
  • You can have try-catch blocks

Is std::sort constexpr?

In order to use a function in a constexpr context, it must be marked constexpr (which comes with a set of restrictions on what you can do in the function; these are discussed below). In C++11, std::sort isn’t constexpr because it breaks those restrictions (and it won’t be constexpr until C++20).

However, if you’re allowed to use C++14, you can write your own sorting function that works at compile time.

Full overview: https://en.cppreference.com/w/cpp/language/constexpr

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Is constant value evaluated at compile time in Go?

分類Dev

How to tell Angular not to compile some part of template?

分類Dev

How to tell if it's British Summer Time

分類Dev

How is a vptr initialized at compile time?

分類Dev

Unix command to tell how much RAM was used during program runtime?

分類Dev

Does SerNet compile Samba 4 with CUPS support? (How to tell in general?)

分類Dev

How to tell Drools to "JIT"-compile all the MVEL to Java?

分類Dev

Not compile-time constant expression in VS2017

分類Dev

postgresql regexp_replace: how to replace captured group with evaluated expression (adding an integer value to capture group)

分類Dev

Keras model.compile: metrics to be evaluated by the model

分類Dev

How is an Or statement evaluated in VBA?

分類Dev

How is A *= B *= A *= B evaluated?

分類Dev

How to enforce that a type implements a trait at compile time?

分類Dev

How to order types at compile-time?

分類Dev

how to check the size of an array during compile time

分類Dev

How to check at compile time if type is polymorhic

分類Dev

How do I tell the `date` command to only show the time?

分類Dev

How to tell if two dates are equal while ignoring time?

分類Dev

How to write a grammartically correct MySQL expression that generates a runtime error?

分類Dev

why for-loop isn't a compile time expression and extended constexpr allows for-loop in a constexpr function

分類Dev

How is shift operator evaluated in C?

分類Dev

How to ensure compile-time safety in custom data structures

分類Dev

How can you compare two character strings statically at compile time

分類Dev

How to get compile time error checking with real valued function arguments?

分類Dev

How to create compile time check for multiple C++ types?

分類Dev

How can I use std::make_tuple at compile time?

分類Dev

How to create compile-time constant in Kotlin from enum?

分類Dev

How to use GenericArray with a limited range of length that can be detected at compile time?

分類Dev

How to use GenericArray with a limited range of length that can be detected at compile time?

Related 関連記事

  1. 1

    Is constant value evaluated at compile time in Go?

  2. 2

    How to tell Angular not to compile some part of template?

  3. 3

    How to tell if it's British Summer Time

  4. 4

    How is a vptr initialized at compile time?

  5. 5

    Unix command to tell how much RAM was used during program runtime?

  6. 6

    Does SerNet compile Samba 4 with CUPS support? (How to tell in general?)

  7. 7

    How to tell Drools to "JIT"-compile all the MVEL to Java?

  8. 8

    Not compile-time constant expression in VS2017

  9. 9

    postgresql regexp_replace: how to replace captured group with evaluated expression (adding an integer value to capture group)

  10. 10

    Keras model.compile: metrics to be evaluated by the model

  11. 11

    How is an Or statement evaluated in VBA?

  12. 12

    How is A *= B *= A *= B evaluated?

  13. 13

    How to enforce that a type implements a trait at compile time?

  14. 14

    How to order types at compile-time?

  15. 15

    how to check the size of an array during compile time

  16. 16

    How to check at compile time if type is polymorhic

  17. 17

    How do I tell the `date` command to only show the time?

  18. 18

    How to tell if two dates are equal while ignoring time?

  19. 19

    How to write a grammartically correct MySQL expression that generates a runtime error?

  20. 20

    why for-loop isn't a compile time expression and extended constexpr allows for-loop in a constexpr function

  21. 21

    How is shift operator evaluated in C?

  22. 22

    How to ensure compile-time safety in custom data structures

  23. 23

    How can you compare two character strings statically at compile time

  24. 24

    How to get compile time error checking with real valued function arguments?

  25. 25

    How to create compile time check for multiple C++ types?

  26. 26

    How can I use std::make_tuple at compile time?

  27. 27

    How to create compile-time constant in Kotlin from enum?

  28. 28

    How to use GenericArray with a limited range of length that can be detected at compile time?

  29. 29

    How to use GenericArray with a limited range of length that can be detected at compile time?

ホットタグ

アーカイブ