C ++ 11ã®æ°æ©èœã®1ã€ã«
constexpr
ã ããã䜿çšããŠãã³ã³ãã€ã«æ®µéã§èšç®ãããå€æ°ãé¢æ°ãããã«ã¯ãªããžã§ã¯ããäœæã§ããŸãã 以åã¯ããã®ãããªç®çã§ãã³ãã¬ãŒãã䜿çšããå¿
èŠããã£ããããããã¯äŸ¿å©ã§ãã ãããããã¹ãŠãããã»ã©åçŽã§ã¯ãããŸããã
constexpr
ããŸãæ
£ããŠããªã人ã¯ãã³ã³ãã€ã«æ®µéã§ã®èšç®ã«åé¡ã¯ãªããšããå°è±¡ãåãããããããŸããã ãã ãã
constexpr
åŒã«ã¯å³ããå¶éããããŸãã
æåã®éšåã§ã¯
constexpr
ã«ã€ããŠãC ++ 14æšæºã§ã©ã®ãããªå€æŽãè¡ããããã«ã€ããŠ
constexpr
ã2çªç®ã®éšåã§ã¯
constexpr
ã®äœ¿çšäŸã
constexpr
ãŸããæååå
ã®æ°åŒã®çµæãèæ
®ããã©ã€ãã©ãªã§ãã
ããã䜿çšããŠã次ã®ã³ãŒããèšè¿°ã§ããŸãã
constexpr auto x = "(4^2-9)/8+2/3"_solve; std::cout << "Answer is " << x;
ãããŠãåæ°ã®åœ¢åŒã§ã®åçã¯ãã³ã³ãã€ã«æ®µéã§åä¿¡ãããŸãã
Answer is 37/24
ããã«èŠåããŸããããã®ã©ã€ãã©ãªã®ã³ãŒãã¯ç解ãã«ããã§ãã
ãã®ãããã¯ãèå³æ·±ã人ã«ã¯ãã«ããã®äžã§æè¿ããŸãïŒ
constexprãšã¯äœã§ããïŒ
ãŸãã
constexpr
äœã§ãããã«ã€ããŠã®ããã€ãã®èšèã æ¢ã«è¿°ã¹ãããã«ãããã䜿çšãããšãã³ã³ãã€ã«æ®µéã§ããã€ãã®æäœãå®è¡ã§ããŸãã 次ã®ããã«ãªããŸãã
constexpr int sum (int a, int b) { return a + b; } void func() { constexpr int c = sum (5, 12);
constexpré¢æ°
constexpr _ _ ()
é¢æ°ã®åã«C ++ 11ã§è¿œå ããã
constexpr
ã¯ãã³ã³ãã€ã«æ®µéã§ãã©ã¡ãŒã¿å€ãèšç®ã§ããå Žåãè¿ãããå€ãã³ã³ãã€ã«æ®µéã§èšç®ããå¿
èŠãããããšãæå³ããŸãã ã³ã³ãã€ã«æ®µéã§å°ãªããšã1ã€ã®ãã©ã¡ãŒã¿ãŒã®å€ãäžæãªå Žåãé¢æ°ã¯ã©ã³ã¿ã€ã ã§èµ·åãããŸãïŒã³ã³ãã€ã«ãšã©ãŒã¯åºåãããŸããïŒã
constexprå€æ°
constexpr = expression;
ãã®å Žåã®ããŒã¯ãŒãã¯ãå®æ°ãäœæããããšãæå³ããŸãã ããã«ãåŒã¯ã³ã³ãã€ã«æ®µéã§èªèãããå¿
èŠããããŸãã
ãã®äŸãèããŠã¿ãŸãããïŒ
int sum (int a, int b) { return a + b; } constexpr int new_sum (int a, int b) { return a + b; } void func() { constexpr int a1 = new_sum (5, 12);
constexpr
å€æ°ã¯å®æ°ïŒ
const
ïŒã§ãããå®æ°ã¯constexprå€æ°ã§ã¯ãããŸããã
å€æ°ã®
constexpr
ãã倱ããããå Žåãã³ã³ãã€ã«æ®µéã§å€ãèšç®ã§ãããšããŠãè¿ãããŸããã constexprã¯cvæå®åã§ã¯ãªããããconst_castã䜿çšããŠconstexpræå®åãè¿œå ããããšã¯ã§ããŸããïŒ
const
ããã³
volatile
ïŒã ãã®ãããªã³ãŒãã¯æ©èœããŸããïŒ
constexpr int inc (int a) { return a + 1; } void func() { int a = inc (3); constexpr int b = inc (a);
é¢æ°ãã©ã¡ãŒã¿ãŒã
constexpr
ããããšã¯ã§ããŸããã ã€ãŸããã³ã³ãã€ã«æ®µéã§ã®ã¿æ©èœãã
constexpr
é¢æ°ã®ã¿ãäœæããããšã¯ã§ããŸããã
constexpr
é¢æ°ãã¯ã©ã¹ã§æ©èœããŸããããã«ã€ããŠã¯åŸã§èª¬æããŸãã
GCCã¯ãããŒãžã§ã³4.4ãã
constexpr
ãµããŒãããClangã¯ããŒãžã§ã³2.9ããããµããŒãããVisual Studio 2013ã¯ãµããŒãããŸããïŒãã ããCTPã¯Visual Studioâ 14âã§ãµããŒããè¿œå ããŸããïŒã
å¶éäºé
ãããã©ãã»ã©äŸ¿å©ããç解ããã®ã§ãããããã®æšœã®äžã®è»èã«ããšãè¿œå ã§ããŸãã ãããŠãããªã倧ããªã¹ããŒã³ã
constexpr
å€æ°ã®å¶çŽããå§ããŸãããã constexprå€æ°ã®åã¯ããªãã©ã«åãã€ãŸã次ã®ããããã§ãªããã°ãªããŸããã
- ã¹ã«ã©ãŒå
- ãã€ã³ã¿
- ã¹ã«ã©ãŒåã®é
å
- 以äžã®æ¡ä»¶ãæºããã¯ã©ã¹ïŒ
- ããã©ã«ãã®ãã¹ãã©ã¯ã¿ããããŸã
- ãã¹ãŠã®ééçã¯ã©ã¹ã¡ã³ããŒã¯ãªãã©ã«åã§ããå¿
èŠããããŸãã
- ã¯ã©ã¹ã«ã¯å°ãªããšã1ã€ã®
constexpr
ãå¿
èŠã§ãïŒãã ããã³ããŒããã³ç§»åã³ã³ã¹ãã©ã¯ã¿ãŒã¯å¿
èŠãããŸããïŒããŸãã¯ãã³ã³ã¹ãã©ã¯ã¿ãŒããŸã£ãããããŸããã
constexpr
-variableã¯ã次ã®æ¡ä»¶ãæºããå¿
èŠããããŸãã
- ãã®åã¯ãªãã©ã«ã§ãªããã°ãªããŸãã
- ããã«å€ãå²ãåœãŠããã
constexpr
å¿
èŠããããŸã - ã³ã³ã¹ãã©ã¯ã¿ãŒãã©ã¡ãŒã¿ãŒãŸãã¯å²ãåœãŠãããå€ã«ã¯ããªãã©ã«ãŸãã¯
constexpr
-variablesããã³constexpr
constexpr
ã®ã¿ãå«ããããšãã§ããŸã
çããããšã¯äœããªãããã§ãã äž»ãªå¶éã¯constexpré¢æ°ã«èª²ããããŸãïŒ
- ä»®æ³ã«ããããšã¯ã§ããŸãã
- ãªãã©ã«åãè¿ãå¿
èŠããããŸãïŒ
void
ãè¿ãããšã¯ã§ããŸãã* ïŒ - ãã¹ãŠã®ãã©ã¡ãŒã¿ãŒã¯ãªãã©ã«åã§ããå¿
èŠããããŸãã
- é¢æ°ã®æ¬äœã«ã¯ã次ã®ã¿ãå«ããå¿
èŠããããŸãã
static_assert
typedef
ãŸãã¯using
ãã¯ã©ã¹ãšenum
åïŒ enum
ïŒãé€ããã¹ãŠã®åã宣èšããŸã- ååãŸãã¯åå空éïŒ
namespace
ïŒã®å¯èŠæ§ã瀺ãããã«using
- ãªãã©ã«ãŸãã¯
constexpr
å€æ°ãšconstexpr
constexpr
ã®ã¿ãå«ãããšãã§ããæ£ç¢ºã« 1ã€ã®return
* C ++ 14ã§ã¯ã void
ããªãã©ã«åã«ãªããŸããconstexpr
ã¯ã
return
ç¯ãš1ã€ã®æ°ããç¯ã®è¿œå ãé€ããŠãé¢æ°ãšåãå¶éã«åŸããŸãã
ãã¹ãŠã®ééçã¯ã©ã¹ã¡ã³ããŒãšåºåºã¯ã©ã¹ã®ã¡ã³ããŒã¯ãäœããã®æ¹æ³ã§åæåããå¿
èŠããããŸãïŒå®£èšæã«åæåãªã¹ããŸãã¯ã¯ã©ã¹ã¡ã³ããŒã®åæåã䜿çšããã³ã³ã¹ãã©ã¯ã¿ãŒã§ïŒããããã«å²ãåœãŠãããåŒã«ã¯ããªãã©ã«ãŸãã¯
constexpr
-variablesããã³
constexpr
constexpr
ã®ã¿ãå«ããå¿
èŠããããŸãã
é¢æ°å
ã®å€æ°ãåæåããã
if-else
ã«ãŒãã
if-else
äœæããããšã¯ã§ããŸããã äžæ¹ã§ããããã®å¶éã¯ãã³ã³ãã€ã©ãŒãã³ã³ãã€ã«äžã«äœããã®åœ¢ã§ããã°ã©ã ã®å®è¡ãã¢ãã¿ãŒããå¿
èŠããããšããäºå®ã«ãããã®ã§ãïŒååž°ã¯ã«ãŒããããå²ã蟌ã¿ã容æã§ãïŒã äžæ¹ãè€éãªé¢æ°ã®äœæã«ã¯åé¡ããããŸãã
ãã¡ããããã¹ãŠåãã§ããããã®å¯èœæ§ã¯ãã¹ãŠå®çŸã§ããŸãã ã«ãŒãã®ä»£ããã«ååž°ã䜿çšãã
if-else
代ããã«ãã
? :
? :
"ããããŠãå€æ°ãäœæãã代ããã«ãé¢æ°ã®å€ã䜿çšããŸãã
ããã¯ãã¹ãŠãé¢æ°åããã°ã©ãã³ã°ã«éåžžã«äŒŒãŠããŸãã é¢æ°åããã°ã©ãã³ã°èšèªã§ã¯ãååãšããŠãå€æ°ã¯éå§ã§ãããã«ãŒãããããŸããã å®éãé¢æ°ãåŒã³åºãããšãã§ããé¢æ°ãžã®ãã€ã³ã¿ãŒã䜿çšããŠé«æ¬¡ã®é¢æ°ãå®è£
ããããšãã§ããŸãïŒæ®å¿µãªãããconstexpræ§é ã§ã¯å¿åé¢æ°ïŒã©ã ãïŒã¯äœ¿çšã§ããŸããïŒã ãŸãããã¹ãŠã®constexpré¢æ°ã¯çŽç²ãªé¢æ°ã§ãïŒãã©ã¡ãŒã¿ãŒã«ã®ã¿äŸåããçµæã®ã¿ãè¿ããŸãïŒã
constexpr
ã¢ã«ãŽãªãºã ãäœæããã«ã¯ãå°ãªããšãé¢æ°åããã°ã©ãã³ã°ã®åæç¥èãå¿
èŠã§ãã
ããããããã§C ++ã«ã¯æ§æã«å€§ããªåé¡ããããŸããå¿åé¢æ°ã¯äœ¿çšã§ãããé¢æ°ã®ãã¹ãŠã®ã¢ã¯ã·ã§ã³ã¯1ã€ã®é·ãåŒã§ãããã
? :
? :
»ã³ãŒãã¯ãŸã£ããèªããªããªããŸãã ãŸããããããã¹ãŠã«ã¯ãæ°çŸè¡ãå ããå¯èœæ§ã®ããäžå¯è§£ãªãšã©ãŒã¡ãã»ãŒãžã䌎ããŸãã
ããããåé¡ã¯ããã ãã§ã¯çµãããŸããã åŸã§ãã䜿çšãã
constexpr
é¢æ°ãäœæããå Žåãèªã¿åãå¯èœãªãšã©ãŒãè¿ããšäŸ¿å©ã§ãã ããã§ã¯ã
static_assert
ãããã«ã¡ããã©é©åã§ãããšèª€ã£ãŠæ³å®ãããŸãã ãã ããé¢æ°ãã©ã¡ãŒã¿ã¯
constexpr
ã«ã§ããªãããã
static_assert
ã¯äœ¿çšã§ããŸãããããã¯ãã³ã³ãã€ã«æã«ãã©ã¡ãŒã¿å€ãèªèãããããšãä¿èšŒãããªãçç±ã§ãã
ãšã©ãŒã衚瀺ããæ¹æ³ã¯ïŒ ç§ãèŠã€ããå€ããå°ãªããéåžžã®æ¹æ³ã¯ãäŸå€ãã¹ããŒããããšã§ãïŒ
constexpr int div (int x, int y) { return (y == 0) ? throw std::logic_error ("x can't be zero") : (y / x); }
ã³ã³ãã€ã«äžã®é¢æ°åŒã³åºãã®å Žåãconstructexpré¢æ°ã«throwæ§æãå«ããããšãã§ããªããšãããšã©ãŒã衚瀺ãããå®è¡æã«é¢æ°ãäŸå€ãã¹ããŒããŸãã
ééããèŠã€ããã®ã¯é£ããã§ãããããå°ãªããšãäœãã
gcc 4.8.2ã®ãšã©ãŒäŸMain.cppïŒ16ïŒ24ïŒ 'MathCpp :: operator ""ã®constexprå±éã§ïŒïŒïŒconst char *ïŒ "ïŒ67 + 987 ^ïŒ7-3 * 2ïŒïŒ*ïŒ34-123ïŒ+ 17 ^ 2/0 +ïŒ-1ïŒ "ïŒã37ulïŒ '
MathCpp.hïŒ115ïŒ28ïŒ 'MathCppã®constexprå±éïŒ:: solveïŒstrãïŒïŒsize_tïŒsizeïŒïŒ'
MathCpp.hïŒ120ïŒ103ïŒ 'MathCpp :: get_addsubïŒMathCpp :: SMathDataïŒstrãïŒïŒintïŒsizeïŒã0ïŒïŒ'ã®constexpræ¡åŒµ
MathCpp.hïŒ209ïŒ89ïŒ 'MathCpp :: const getr addsubïŒdata.MathCpp :: SMathData :: createïŒïŒïŒïŒintïŒMathCpp :: get_muldivïŒdataïŒ.MathCpp :: SMathValue :: endïŒ+ 1ïŒïŒãMathCpp :: get_muldivïŒdataïŒ.MathCpp :: SMathValue :: valueïŒ '
MathCpp.hïŒ217ïŒ50ïŒ 'MathCpp :: get_muldivïŒdata.MathCpp :: SMathData :: createïŒïŒïŒïŒintïŒdata.MathCpp :: SMathData :: startïŒ+ 1ïŒïŒïŒã®constexpræ¡åŒµ
MathCpp.hïŒ181ïŒ83ïŒ 'MathCpp :: _ get_muldivïŒdata.MathCpp :: SMathData :: createïŒïŒïŒïŒintïŒMathCpp :: get_powïŒdataïŒ.MathCpp :: SMathValue :: endïŒ+ã®constexpræ¡åŒµã§1ïŒïŒãMathCpp :: get_powïŒdataïŒ.MathCpp :: SMathValue :: valueïŒ '
MathCpp.hïŒ38ïŒ111ïŒãšã©ãŒïŒåŒ '<throw-expression>'ã¯å®æ°åŒã§ã¯ãããŸãã
#define math_assertïŒconditionãdescriptionïŒïŒïŒconditionïŒïŒINVALID_VALUEïŒïŒthrow std :: logic_errorïŒdescriptionïŒãINVALID_VALUEïŒïŒ
^
MathCpp.hïŒ195ïŒ15ïŒæ³šïŒãã¯ããmath_assertãã®å±éäž
ïŒ math_assertïŒfalseãããŒãé€ç®ãïŒ
ãã®ãšã©ãŒåºåæ¹æ³ã¯ãŸã èšèªæšæºã«æºæ ããŠããŸãã;ã³ã³ãã€ã©ã
constexpr
é¢æ°ã§
constexpr
䜿çšã§ããªããšãããšã©ãŒãåžžã«ã¹ããŒããããšãçŠæ¢ãããã®ã¯äœããããŸããã GCC 4.8.2ã§ã¯ããã¯æ©èœããŸãããVisual Studio "14"ã§ã¯CTP C ++ã³ã³ãã€ã©ã¯ãããããŸããã
ãã®çµæãæžãã®ãé£ããããããã°ããã®ãé£ããããã®ãããªæ§é ãç解ããã®ãé£ããã§ãã
ãããããã¹ãŠãããã»ã©æªãã¯ãããŸãããC++ 14ã§ã¯ãéåžžã«å€ãã®å¶éãåé€ãããŸãã
C ++ 14ã§ã®å€æŽ
æ¢ã«è¿°ã¹ãããã«ãæ°ããæšæºã§ã¯ã
void
ããªãã©ã«åã«ãªããããšãã°ããã©ã¡ãŒã¿ãŒå€ã®æ£åœæ§ããã§ãã¯ããé¢æ°ãäœæã§ããããã«ãªããŸããã
2çªç®ã®å°ããªå€æŽç¹ã¯ã
constexpr
ã¯ã©ã¹ã®ã¡ã³ããŒé¢æ°ãå®æ°ã§ã¯ãªããªã£ãããšã§ãã
C ++ 11ã§ã¯ã次ã®è¡ã¯åçã§ããããC ++ 14ã§ã¯ããã¯åœãŠã¯ãŸããŸããã
class car { constexpr int foo (int a);
ããã®èª¬æã¯
ããã§èŠã€ããããšãã§ã
ãŸã ã
ãããŠæåŸã«ãäž»ãªå€æŽã«ããã
constexpr
é¢æ°ããã³ã³ã³ã¹ãã©ã¯ã¿ãŒã§ã»ãŒãã¹ãŠã®æ§æã䜿çšã§ããããã«ãªããŸããã
constexpr
é¢æ°ã®æ¬äœã«ã¯ã
constexpr
ãé€ãä»»æã®æ§æäœãå«ããããšãã§ããŸãã
- ã¢ã»ã³ããªã€ã³ãµãŒã
- ããŒã¯ãŒã
goto
- éãªãã©ã«åå€æ°ããŸãã¯
static
ããã³thread_safe
å€æ°ã®å®çŸ©ã ãã¹ãŠã®å€æ°ã¯ãå®çŸ©äžã«åæåããå¿
èŠããããŸãã
ãããŠ
constexpr
æ¬äœã¯ãããå¿ å®ãªæ¡ä»¶ãæºãããŠããã¯ãã§ãã
constexpr
é¢æ°ã®ãã¹ãŠã®æ¡ä»¶ãæºããå¿
èŠããããŸã- ãã¹ãŠã®ééçã¡ã³ããŒã¯ãªãã©ã«åã§ããå¿
èŠããããŸãã
- åæ§ã®æ¡ä»¶ã¯ããã¹ãŠã®ééçã¯ã©ã¹ã¡ã³ããŒãäœããã®æ¹æ³ã§åæåããå¿
èŠãããããšã§ãã
union
ã䜿çšã§ããŸãããããã€ãã®å¶éããããŸã
ãã®çµæãC ++ 14ããµããŒãããã³ã³ãã€ã©ãç»å ŽããåŸãéåžžã®é¢æ°ãšã»ãšãã©å€ãããªãconstexpré¢æ°ãèšè¿°ã§ããããã«ãªããŸãã ãããŸã§ã®éãéåžžã«ãããã«ããã³ãŒããäœæããå¿
èŠããããŸãã
C ++ 11ã§constexprã䜿çšããäŸ
constexprã®äœ¿çšäŸãšããŠãæååã«ããæ°åŒã®çµæãèªã¿åãã©ã€ãã©ãªãæäŸããŸãã
ãã®ããã次ã®ãããªã³ãŒããèšè¿°ã§ããããã«ããŸãã
constexpr auto n = "(67+987^(7-3*2))*(34-123)+17^2+(-1)"_solve; std::cout << "Answer is " << n;
å¥ã®æ°ããC ++ 11æ©èœãããã§äœ¿çšãããŸãïŒã«ã¹ã¿ã ãªãã©ã«ã ãã®å Žåãçµæã®å€ã
constexpr
å€æ°ã«å²ãåœãŠãããŠããå Žåã§ããã³ã³ãã€ã«æ®µéã§é¢æ°ãåŒã³åºãããããšãä¿èšŒãããŠããããããããã¯
constexpr
ã§ãã
ã«ã¹ã¿ã ãªãã©ã«ã¯æ¬¡ã®ããã«å®£èšãããŸãã
constexpr int operator "" _solve (const char* str, const size_t size); constexpr int solve (const char* str, const size_t size); constexpr int operator "" _solve (const char* str, const size_t size) { return solve (str, size); }
次ã®ãã¯ããã¢ãµãŒããšããŠäœ¿çšãããŸãã
#define math_assert(condition,description) ((condition) ? 0 : (throw std::logic_error (description), 0))
ã©ã€ãã©ãªã¯ãå ç®ãæžç®ãä¹ç®ãé€ç®ãã¬ã€ãºãã§ããŸãããŸããæ¬åŒ§ã®ãµããŒãããããŸãã ããã¯ãååž°éäžã䜿çšããŠå®è£
ãããŸãã
ãªãã¬ãŒã¿ãŒã®åªå
é äœã¯æ¬¡ã®ãšããã§ãïŒé«ããã®ããäœããã®ãžïŒã
- å ç®ãšæžç®
- ä¹ç®ãšé€ç®
- ã¹ãä¹
æ°å€ã床ãåèšãªã©ãèªã¿åãé¢æ°ã¯ã1ã€ã®ãã©ã¡ãŒã¿ãŒïŒ
SMathData
æ§é äœïŒãåããŸãã æååããµã€ãºã
start
å€æ°ãä¿åããŸã-èªã¿åããéå§ããå ŽæïŒ
struct SMathData { constexpr SMathData (const char* _str, const int _size, const int _start) : str (_str), size (_size), start (_start) {} constexpr SMathData create (const int _start) const { return SMathData (str, size, _start); } constexpr char char_start() const { return char_at (start); } constexpr char char_at (const int pos) const { return (pos >= 0 && pos < size) ? str[pos] : ((pos == size) ? 0 : (math_assert (false, "Internal error: out of bounds"), 0)); } const char* str; const int size; const int start; };
ãããŠããããã®é¢æ°ã¯
SMathValue
æ§é äœã
SMathValue
ãŸãã ã«ãŠã³ããããå€ã¯ãã®äžã«ä¿åãããendã¯ãæ°å€ãéé¡ã補åããŸãã¯äœãã®çµãããæžã蟌ãŸããå€æ°ã§ãã
struct SMathValue { constexpr SMathValue (const int _value, const int _end) : value (_value), end (_end) {} constexpr SMathValue add_end (int dend) const { return SMathValue (value, end + dend); } const int value; const int end; };
çªå·ãèªã¿åãã«ã¯ã3ã€ã®æ©èœïŒã¡ã€ã³1ã€ãšè£å©2ã€ïŒããããŸãã
ããã¯éåžžã«è€éãªèšèšã§ãã
get_number
ã¯ãçŸåšã®ã€ã³ããã¯ã¹ãæå¹ã§ããããšã確èªãã
get_number
ãåŒã³åºããŠãæåã®å埩ãšããŠæ°å€ã®æ«å°Ÿãæž¡ããŸãïŒæ°å€ã¯å³ããå·Šã«èªã¿åãããŸãïŒã
ãã©ã±ããã䜿çšããïŒ
çŸåšã®ã€ã³ããã¯ã¹ã«çªå·ãããå Žåãé¢æ°ã¯
get_number
åŒã³åºã
get_number
ããã以å€ã®å Žåãé¢æ°ã¯æ¬åŒ§å
ã®åŒãèæ
®ããŸãã
次ã«ãã¹ãä¹é¢æ°ããããŸãã
_get_pow
é¢æ°ã¯ãçŸåšã®æåã
'^'
ããšã確èªããŸãã ãã®å Žåãé¢æ°ã¯ããèªäœïŒããæ£ç¢ºã«ã¯
get_pow
ïŒãåŒã³åºãã
get_pow
ã«ã
get_pow
ã®ç¯å²ã®valueãšçããæ°ããå€ãæž¡ããŸãã
æåå
"25"
ã
get_pow
åŒã³åºãããå Žåã«æ£ããåŠçãããããšã
get_pow
ãŸããã ãã®å Žåãçªå·ã¯åã«èªã¿åãããããããã®åŸè¿ãããŸãã
math_pow
ã¯ãæŽæ°ã«äžããåçŽãª
constexpr
é¢æ°ã§ãã
Math_powã®å®è£
constexpr int math_pow (const int x, const int y); constexpr int _math_pow (const int x, const int y, const int value); constexpr int math_pow (const int x, const int y) { return math_assert (y >= 0, "Power can't be negative"), _math_pow (x, y.to_int(), 1); } constexpr int _math_pow (const int x, const int y, const int value) { return (y == 0) ? value : (x * _math_pow (x, y - 1, value)); }
補åãšéšéã¯1ã€ã®æ©èœã§åŠçãããŸãã
ãã®æ§é ãç解ããããšã¯ããªãé£ãããæžãããšãå°é£ã§ãã ããã§ãçŸåšã®æåã
'*'
ã§ãããã©ããã確èªããŸããããã§ããå Žåãé¢æ°ã¯
value
ãèªã¿åãçªå·ïŒãŸãã¯åŒïŒã§ä¹ç®ããŠèªåèªèº«ãåŒã³åºããŸãã
'/'
é¢æ°ã¯åæ§ã«åäœ
'/'
ãŸãã
'/'
åã«åæ¯ããŒãã«çãããªããšãããã§ãã¯ããããŸãã çŸåšã®æåã
'*'
ãŸãã¯
'/'
ã§ãªãå Žåãå€ãåã«è¿ãããŸãã
åæ§ã«ãåèšãšå·®ã§ãèµ·ãããŸãïŒ
Get_addsubã®å®è£
constexpr SMathValue get_addsub (const SMathData data); constexpr SMathValue _get_addsub (const SMathData data, const CMathVariable value); constexpr SMathValue get_addsub (const SMathData data) { return _get_addsub (data.create (get_muldiv (data).end + 1), get_muldiv (data).value); } constexpr SMathValue _get_addsub (const SMathData data, const CMathVariable value) { return (data.char_start() == '+') ? _get_addsub (data.create
get_muldiv
ããã³
_getmuldiv
ãããã
get_muldiv
ããã³
_getmuldiv
ãšåæ§ã«
_getmuldiv
ãŸãã
ãããŠæåŸã«ãé¢æ°
solve
ãå®è£
ããããšã¯æ®ã£ãŠããŸãïŒ
constexpr CMathVariable solve (const char* str, const size_t size);
æåŸã«ã§ããããšã¯ãååãšåæ¯ãå¥ã
ã®å€æ°ãšããŠä¿åããæ°å€ã®ã¯ã©ã¹ã䜿çšããããšã§ãã ç¹å¥ãªãã®ã¯ãããŸããããã¹ãŠã®é¢æ°ãšã³ã³ã¹ãã©ã¯ã¿ãŒã«
constexpr
ã
constexpr
ã
ç¬èªã®æ°åã®ã¯ã©ã¹ class CMathVariable { private: int64_t numerator_; uint64_t denominator_; constexpr CMathVariable (int64_t numerator, uint64_t denominator); constexpr int64_t sign_ (int64_t a) const; constexpr uint64_t gcd_ (uint64_t a, uint64_t b) const; constexpr CMathVariable reduce_() const; public: constexpr explicit CMathVariable (int number); constexpr CMathVariable operator + (const CMathVariable& n) const; constexpr CMathVariable operator - (const CMathVariable& n) const; constexpr CMathVariable operator * (const CMathVariable& n) const; constexpr CMathVariable operator / (const CMathVariable& n) const; constexpr int64_t numerator() const; constexpr uint64_t denominator() const; constexpr bool is_plus_inf() const; constexpr bool is_menus_inf() const; constexpr bool is_nan() const; constexpr bool is_inf() const; constexpr bool is_usual() const; constexpr bool is_integer() const; constexpr int to_int() const; constexpr int force_to_int() const; constexpr double to_double() const; friend constexpr CMathVariable operator - (const CMathVariable& n); friend constexpr CMathVariable operator + (const CMathVariable& n); friend std::ostream& operator << (std::ostream& os, const CMathVariable& var); }; constexpr CMathVariable operator - (const CMathVariable& n); constexpr CMathVariable operator + (const CMathVariable& n); std::ostream& operator << (std::ostream& os, const CMathVariable& var); constexpr CMathVariable::CMathVariable (int number) : numerator_ (number), denominator_ (1) { } constexpr CMathVariable::CMathVariable (int64_t numerator, uint64_t denominator) : numerator_ (numerator), denominator_ (denominator) { } constexpr int64_t CMathVariable::sign_ (int64_t a) const { return (a > 0) - (a < 0); } constexpr uint64_t CMathVariable::gcd_ (uint64_t a, uint64_t b) const { return (b == 0) ? a : gcd_ (b, a % b); } constexpr CMathVariable CMathVariable::reduce_() const { return (numerator_ == 0) ? CMathVariable (0, sign_ (denominator_)) : ((denominator_ == 0) ? CMathVariable (sign_ (numerator_), 0) : CMathVariable (numerator_ / gcd_ (static_cast<uint64_t> (std::abs (numerator_)), denominator_), denominator_ / gcd_ (static_cast<uint64_t> (std::abs (numerator_)), denominator_))); } constexpr int64_t CMathVariable::numerator() const { return numerator_; } constexpr uint64_t CMathVariable::denominator() const { return denominator_; } constexpr bool CMathVariable::is_plus_inf() const { return denominator_ == 0 && numerator_ > 0; } constexpr bool CMathVariable::is_menus_inf() const { return denominator_ == 0 && numerator_ < 0; } constexpr bool CMathVariable::is_nan() const { return denominator_ == 0 && numerator_ == 0; } constexpr bool CMathVariable::is_inf() const { return denominator_ == 0 && numerator_ != 0; } constexpr bool CMathVariable::is_usual() const { return denominator_ != 0; } constexpr bool CMathVariable::is_integer() const { return denominator_ == 1; } constexpr int CMathVariable::to_int() const { return static_cast<int> (numerator_ / denominator_); } constexpr int CMathVariable::force_to_int() const { return (!(denominator_ == 1 && static_cast<int> (numerator_) == numerator_) ? (throw std::logic_error ("CMathVariable can't be represented by int"), 0) : 0), to_int(); } constexpr double CMathVariable::to_double() const { return static_cast<double> (numerator_) / denominator_; } constexpr CMathVariable CMathVariable::operator + (const CMathVariable& n) const { return CMathVariable ( static_cast<int64_t> (n.denominator_ / gcd_ (denominator_, n.denominator_)) * numerator_ + static_cast<int64_t> (denominator_ / gcd_ (denominator_, n.denominator_)) * n.numerator_, denominator_ / gcd_ (denominator_, n.denominator_) * n.denominator_).reduce_(); } constexpr CMathVariable CMathVariable::operator - (const CMathVariable& n) const { return CMathVariable ( static_cast<int64_t> (n.denominator_ / gcd_ (denominator_, n.denominator_)) * numerator_ - static_cast<int64_t> (denominator_ / gcd_ (denominator_, n.denominator_)) * n.numerator_, denominator_ / gcd_ (denominator_, n.denominator_) * n.denominator_).reduce_(); } constexpr CMathVariable CMathVariable::operator * (const CMathVariable& n) const { return CMathVariable ( numerator_ * n.numerator_, denominator_ * n.denominator_).reduce_(); } constexpr CMathVariable CMathVariable::operator / (const CMathVariable& n) const { return CMathVariable ( numerator_ * static_cast<int64_t> (n.denominator_) * (n.numerator_ ? sign_ (n.numerator_) : 1), denominator_ * static_cast<uint64_t> (std::abs (n.numerator_))).reduce_(); } constexpr CMathVariable operator + (const CMathVariable& n) { return n; } constexpr CMathVariable operator - (const CMathVariable& n) { return CMathVariable (-n.numerator_, n.denominator_); } std::ostream& operator << (std::ostream& stream, const CMathVariable& var) { if (var.is_plus_inf()) stream << "+inf"; else if (var.is_menus_inf()) stream << "-inf"; else if (var.is_nan()) stream << "nan"; else if (var.denominator() == 1) stream << var.numerator(); else stream << var.numerator() << " / " << var.denominator(); return stream; }
ãã®åŸãååž°éäžã®ã³ãŒãããããã«å€æŽããå¿
èŠãªçµæãååŸããå¿
èŠããããŸããconstexpr-functionsã§ååž°éäžãèšè¿°ããã®ã«çŽ1æ¥ããããŸããããéåžžã®ååž°éäžã¯1æéã§åé¡ãªãæžã蟌ãŸããŸããåé¡ã¯ãæ¬åŒ§ãšã®æ··åããããã°ã®è€éããç解ã§ããªããšã©ãŒããããŠèããããªãã¢ãŒããã¯ãã£ïŒä»ã¯ãååž°çãªéäžã§ãã£ãŠãããã¹ãŠã泚ææ·±ãæ€èšããå¿
èŠããããŸãïŒã§ãããã®ã©ã€ãã©ãªã®ãªããžããªã¯ãã¡ãã§ãïŒhttps : //bitbucket.org/jjeka/mathcppæ¬ ç¹ã質åãããå Žåã¯ãæžããŠãã ããïŒPS C ++ 11/14å°çšã®ãããäœæãããšããæ¥ããšæããŸãã