Docsity

constexpr Applications, Exercises of Architecture

Partial preview of the text

Download constexpr Applications and more Exercises Architecture in PDF only on Docsity!

constexpr: Applications

By Scott Schurr for Ripple Labs at CppCon September 2015

constexpr: Applications

By Scott Schurr for Ripple Labs at CppCon September 2015

constexpr int const_3 = 3 ; // Object definition constexpr double half = 0.5; // Object definition static_assert (half < const_3, "Yipe!"); constexpr char tile_fixative[] = "grout"; // Object definition static_assert (tile_fixative[ 5 ] == '\0', "Yipe!"); void free_func () { constexpr float pi = 3.14159265; // Object definition static_assert ((3.1 < pi) && (pi < 3.2), "Yipe!"); } struct my_struct { // Static data member of literal type static constexpr char who[] = "Gabriel Dos Reis"; static_assert (who[ 0 ] == 'G', "Yipe!"); static constexpr const char* a = &who[ 1 ]; static_assert (*a == 'a', "Yipe!"); }; constexpr Values

  • constexpr declaration allowed on:
    • Free functions
    • Member functions
    • Constructors
  • Allowed code:
    • Constrained in C++
    • Relaxed somewhat in C++
  • constexpr constructor allows user-

defined literal types

constexpr Computations

Compile-time Parsing

  • No native binary literal in C++
  • Can we make one? C++11 binary literal

int main() { using u8_t = std::uint8_t; constexpr u8_t maskA = constexpr11_bin<u8_t>("1110,0000"); constexpr u8_t maskB = constexpr11_bin<u8_t>("0001,1000"); constexpr u8_t maskC = constexpr11_bin<u8_t>("0000,0110"); constexpr u8_t maskD = constexpr11_bin<u8_t>("0000,0001"); static_assert( maskA + maskB + maskC + maskD == 0xFF, "Yipe!"); constexpr double d = constexpr11_bin("1000"); static_assert(d == 8.0, "Yipe!"); return 0 ; } Using constexpr11_bin

  • Much easier to code than in C++
  • Not so useful…
    • C++14 has built-in binary literals
    • 0b1101’ C++14 binary literal
  • What are the limits?
  • What do user errors look like?
  • Do users want runtime execution? More constexpr Questions

Minimum recommended

implementation quantities [Annex B]:

  • Recursive constexpr function

invocations: 512

  • Full-expressions evaluated within a

core constant expression: 1,048,

Actual limits are up to your compiler(s)

constexpr Limits

int main() { auto mask = // <- Not constexpr! constexpr11_bin<std::uint8_t>("1110 0000"); assert(mask == 0xE0); return 0 ; } libc++abi.dylib: terminating with uncaught exception of type std::domain_error: Only '0', '1', and ',' may be used Abort trap: 6

  • Runtime exception for forgetting constexpr User Errors At Runtime

Runtime Execution?

  • Really handy for debugging
  • In this case not so good for users
  • Little reason for runtime conversion
  • Which one causes a runtime error? constexpr auto maskA = constexpr11_bin<std::uint8_t>("1110 0000"); auto maskB = constexpr11_bin<std::uint8_t>("0001 1111");
  • Every invocation has error potential
  • Not within the standard
  • But a hack that sometimes works A Way to Force Compile-Time Only?

Unresolved Symbol In Throw extern const char* compile11_bin_invoked_at_runtime; template < typename T = std::uint32_t> constexpr T compile11_bin( constexpr_txt t, std::size_t i = 0 , // index std::size_t b = 0 , // bit count T x = 0 ) // accumulator { return i >= t.size()? x : // end recursion b >= std::numeric_limits::digits? throw std::overflow_error("Too many bits!") : t[i] == ','? compile11_bin(t, i+1, b, x) : t[i] == '0'? compile11_bin(t, i+1, b+1, (x2)+0) : t[i] == '1'? compile11_bin(t, i+1, b+1, (x2)+1) : throw std::domain_error( // Only '0', '1', and ',' compile11_bin_invoked_at_runtime); }