lundi 12 avril 2021

Can't use constexpr function in static_assert

tell me, please, why I can't use my constexpr recursive function to static_assert if string contains needed count of symbols? Constexpr function supposed to be a constant value, but VS2015 compiller tells me that expression did not evaluate to a constant (error C2131).

constexpr const char* GetRequestStr = "GETVARS:_local_=I,I..8:1-1;NI,NI..8:1-1";

constexpr unsigned RequestedVars(const char* str, unsigned cnt=0){
    return str[0] == 0 ? cnt+1 : RequestedVars(str+1, str[0] == ';' ? ++cnt : cnt);
};
static_assert(RequestedVars(GetRequestStr) == 2, "Must be 2"); // C2131 here

the result (translated into English):

1> d: \ cprojects \ logo \ exchlogo.h (43): error C2131: expression not defined by constant
1> d: \ cprojects \ logo \ exchlogo.h (43): note: the failure was caused by parsing an assignment operation
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 26, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 25, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 24, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 23, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 22, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: skipped intermediate calls: 16 (/ constexpr: backtrace <NUMBER>)
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 5, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 4, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 3, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 2, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (43): note: when calculating "RequestedVars ((" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 ") + 1, 0)"
1> d: \ cprojects \ logo \ exchlogo.h (45): note: when calculating "RequestedVars (" GETVARS: _local_ = I, I..8: 1-1; NI, NI..8: 1-1 " , 0) "
1> d: \ cprojects \ logo \ exchlogo.h (45): error C2131: expression not defined by constant
1> d: \ cprojects \ logo \ exchlogo.h (43): note: the failure was caused by parsing an assignment operation 

Aucun commentaire:

Enregistrer un commentaire