How to time condition at compile time

Martin Schlott

I search for a way to print a message or break a compile run if a headerfile is expired, something like:

#ifndef somemagic(__DATE__ , "2014")
#pragma message("ALARM! Someone should check this file!")
#endif

or is there some new template magic?

To be more specific. I got no way to change the compiler chain. The solution has to be part of the source code. A compiler switch or even add a define in a makefile is not an option.

Note: It is a technical question, which (IMHO) deservers a technical answer. Even if may not fit in all situation, there are reasons where such a technic may be handy.

Angew is no longer proud of SO

You can use the fact that __DATE__ expands to a string literal, and string literals are constant expressions:

static_assert(
  ( 1000 * (__DATE__[7] - '0')
   + 100 * (__DATE__[8] - '0')
   +  10 * (__DATE__[9] - '0')
   +       (__DATE__[10] - '0')
  ) != 2014, "It's 2014!"
);

Live example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set a compile time condition in macros

From Dev

How to use reflection at compile time

From Dev

How is Swift resolving this at compile time?

From Dev

How is Swift resolving this at compile time?

From Dev

How to load XML at compile time

From Dev

How to detect ABI at compile time?

From Dev

How to use a reference such that it results in compile time error or run time error?

From Dev

How to get array size during compile time?

From Dev

How to get the number of members in a class at compile time

From Dev

How to check at compile time if a function is called

From Dev

How can I gate features at compile time?

From Dev

How to force a runtime constant to be a compile time constant?

From Dev

How to check the size of a structure at compile time?

From Dev

How to write Delphi compile-time functions

From Dev

How to loop over a tuple at compile time?

From Dev

How to check, if the class is abstract, at compile time?

From Dev

How to get compile time type of a variable?

From Dev

How to create a static string at compile time

From Dev

How to implement opIndex for compile time indices?

From Dev

How do I validate an annotation at compile time?

From Dev

How to manage compile time dependencies in Maven

From Dev

How to check at compile time if type is polymorhic

From Dev

How to achieve compile time polymorphism of several functions?

From Dev

How to concatenate a const char* in compile time

From Dev

How to compile a function at run-time?

From Dev

How to make GCC evaluate functions at compile time?

From Dev

How to apply an if at compile time in C++

From Dev

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

From Dev

How to initialise a floating point array at compile time?

Related Related

  1. 1

    How to set a compile time condition in macros

  2. 2

    How to use reflection at compile time

  3. 3

    How is Swift resolving this at compile time?

  4. 4

    How is Swift resolving this at compile time?

  5. 5

    How to load XML at compile time

  6. 6

    How to detect ABI at compile time?

  7. 7

    How to use a reference such that it results in compile time error or run time error?

  8. 8

    How to get array size during compile time?

  9. 9

    How to get the number of members in a class at compile time

  10. 10

    How to check at compile time if a function is called

  11. 11

    How can I gate features at compile time?

  12. 12

    How to force a runtime constant to be a compile time constant?

  13. 13

    How to check the size of a structure at compile time?

  14. 14

    How to write Delphi compile-time functions

  15. 15

    How to loop over a tuple at compile time?

  16. 16

    How to check, if the class is abstract, at compile time?

  17. 17

    How to get compile time type of a variable?

  18. 18

    How to create a static string at compile time

  19. 19

    How to implement opIndex for compile time indices?

  20. 20

    How do I validate an annotation at compile time?

  21. 21

    How to manage compile time dependencies in Maven

  22. 22

    How to check at compile time if type is polymorhic

  23. 23

    How to achieve compile time polymorphism of several functions?

  24. 24

    How to concatenate a const char* in compile time

  25. 25

    How to compile a function at run-time?

  26. 26

    How to make GCC evaluate functions at compile time?

  27. 27

    How to apply an if at compile time in C++

  28. 28

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

  29. 29

    How to initialise a floating point array at compile time?

HotTag

Archive