不能在 static_assert 中使用 constexpr 函数

问题描述

请告诉我,如果字符串包含所需的符号数,为什么我不能使用我的 constexpr 递归函数来进行 static_assert? Constexpr 函数应该是一个常量值,但 VS2015 编译器告诉我表达式没有计算为常量(错误 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

结果(翻译成英文):

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,NI..8: 1-1 ") + 25,NI..8: 1-1 ") + 24,NI..8: 1-1 ") + 23,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,NI..8: 1-1 ") + 5,NI..8: 1-1 ") + 4,NI..8: 1-1 ") + 3,NI..8: 1-1 ") + 2,NI..8: 1-1 ") + 1,0)"
1> d: \ cprojects \ logo \ exchlogo.h (45): note: when calculating "RequestedVars (" GEtvarS: _local_ = I,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 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)