微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不能在 static_assert 中使用 constexpr 函数

如何解决不能在 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 举报,一经查实,本站将立刻删除。