c++ - std::initializer_list within constexpr (lookup tables) -


this relates problem i'm trying solve has been addressed couple of times lookup table constexpr; constexpr array , std::initializer_list

i have constexpr function that's slow use run time, purposes want use lookup table, , ideally want use same function @ run time , compile time.

i came this

template<template<size_t>class f, size_t... values> struct lookup_table{    static constexpr auto lookup(size_t i){      auto my_table = {f<values>::value...};     return *(my_table.begin() + i);    }  };    template<size_t n> class some_function{    // terrible way calculate exponential   static constexpr auto slow_exponential(size_t x){     double y = 1 + ((double)x / 1000000.0);     double retval = 1;     (int = 0; < 1000000; i++)       retval *= y;     return retval;   } public:   static constexpr double value = slow_exponential(n); };  int main(int, char**){      using my_table = lookup_table<some_function, 0,1,2,3,4,5,6,7,8,9>;    // test constexprness   constexpr int x =  my_table::lookup(7);   using x = std::integral_constant<int, x>;    std::cout << "enter n" << std::endl;   int n;   std::cin >> n;   std::cout << "exp(" << n << ") = " << my_table::lookup(n) << std::endl;   std::cout << "exp(" << n << ") = " << std::exp(n) << std::endl;    return 0;  } 

this compiles , works expected clang 3.5 i'm not 100% sure it's valid (it doesn't feel constexpr). have strayed undefined behaviour somehow?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -