c++ - template meta-programming for a double multiplication -
i'm trying create method given integer input, makes computations (just multiplication , division) returning result double. method should makes computation @ compile time.
i've tried different approaches this:
template <int n> struct seek { static const double result = 1.0 / (double)n * try<1>::result; }; template <> struct try<1> { static const double result = 1.0; }; but still no success, compile time errors.
edit: i'm not using c++11
credit goes @forever, fixed little error in answer. note nothing guarantees computation happens @ compile time. nil guarantees there is compile time. shamelessly copied reply below.
you cannot initialize variables of not integral type in class const, can constexpr. since cannot utilize c++11, can seek this
template <int n> struct try; template <> struct try<1> { static const double result; }; template <> const double try<1>::result = 1.0; template<int n> struct seek { static const double result; }; template<int n> const double try<n>::result = 1.0 / (double)n * try<1>::result; c++ templates double template-meta-programming c++03
No comments:
Post a Comment