Trädvy Permalänk
Medlem
Registrerad
Sep 2009

compile time c++ || error

Hej, sitter och leker lite med templates och compile time.
Iaf får jag följande error

In file included from main.cpp:2:0: power.h: In instantiation of ‘constexpr const double Factor<2, -899, false>::value’: power.h:21:53: recursively required from ‘constexpr const double Factor<2, -2, false>::value’ power.h:21:53: required from ‘constexpr const double Factor<2, -1, false>::value’ power.h:32:55: required from ‘constexpr const double Factor<2, -2, true>::value’ main.cpp:9:36: required from here power.h:21:53: fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) constexpr static const double value = (double) base*Factor<base, pow-1, false>::value;

och har koden (power.h):

#ifndef _POWER_H_ #define _POWER_H_ /* * * A try for negativ values. * */ template <int base, int pow, bool neg> struct Factor {}; // template <int base, int pow> template <int base, int pow> struct Factor<base, pow, false> { constexpr static const double value = (double) base*Factor<base, pow-1, false>::value; }; // template <int base, int pow> //, bool neg, typename std::enable_if<neg == false>::type * = 0> template <int base, int pow> struct Factor<base, pow, true> { constexpr static const double value = (double) 1/base*Factor<base, pow+1, false>::value; }; // template <int base> template <int base> struct Factor<base,0, false> { constexpr static const double value= 1.0; }; // template <int base> template <int base> struct Factor<base,0, true> { constexpr static const double value= 1.0; }; /* * * This is for only possivtive Values * */ /*template <int base, unsigned int pow> struct Factor { enum{ value = base*Factor<base, pow-1>::value } }; template <int base> struct Factor<base,0> { enum { value = 1 }; };*/ #endif /*_POWER_H_*/

samt main.cpp

#include <iostream> #include "power.h" int main(){ // Negativ values. double twoFour = Factor<2, 4, false>::value; double zero = Factor<2, 0, false>::value; double neg = Factor<2, -2, true>::value; // why 0?? // Possitiv values. /*int twoFour = Factor<2,4>::value; int zero = Factor<2,0>::value;*/ // print the result. std::cout << "2^4 is: " << twoFour << std::endl << "2^0 is: " << zero << std::endl << "2^-2 is: " << neg << std::endl; // end the program. return 0; }

Är det någon som ser vad jag gjort för fel?

Tack för svar!

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6

Trädvy Permalänk
Moderator
Plats
Linköping
Registrerad
Apr 2004
Skrivet av asdfgh:

template <int base, int pow>
struct Factor<base, pow, true>
{
constexpr static const double value = (double) 1/base*Factor<base, pow+1, false>::value;
};

Borde du inte ha true här?

Trädvy Permalänk
Medlem
Registrerad
Sep 2009

aah, var det de jag hade missat, Tack så mycket!

Смерть -это решение всех проблем. Нет человека - нет проблемы
Comp1: Ubuntu 16.04 Comp2: Arch Linux
Comp3: Ubuntu Server 16.04 Comp4: Centos 6.5
Comp5: Linux mint 16 Comp6: Raspberry pi (olika OS hela tiden)
Phone: Motorola Google Nexus 6