constexpr函数中使用OpenMP指令的版本兼容性问题咨询
constexpr函数中使用OpenMP指令的版本兼容性问题咨询
嗨,这个问题我之前也碰到过类似的情况,咱们来捋清楚前因后果:
根据OpenMP 2.1规范的「Directive Format」章节:
Directives may not appear in constexpr functions or in constant expressions. Variadic parameter packs cannot be expanded into a directive or its clauses except as part of an expression argument to be evaluated by the base language, such as into a function call inside an if clause.
其实早从OpenMP 2.1开始就明确规定了OpenMP指令不能出现在constexpr函数或者常量表达式中,只不过旧版本的g++(比如你提到的14.2及之前)没有严格执行这个规范,所以编译时没给你弹出警告。
而g++15开始对OpenMP规范的兼容性检查收紧了,严格遵循了这一条款,所以你现在再在constexpr函数里用OpenMP指令就会触发编译提示了。
如果还想在需要constexpr的场景下实现并行逻辑,建议你调整下代码结构——要么把OpenMP指令移出constexpr函数,要么换用其他符合规范的并行实现方式哦。
备注:内容来源于stack exchange,提问作者Chameleon
相关产品推荐
相关产品推荐

