使用c ++ 14构建时可以使用pack fold表达式(c ++ 17扩展名) [英] pack fold expression (c++17 extension) available when building with c++14

查看:81
本文介绍了使用c ++ 14构建时可以使用pack fold表达式(c ++ 17扩展名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码包含一个fold表达式,afaiu是a的c ++ 17功能:

The following code contains a fold expression, which afaiu is a c++17 feature:

template <typename... T> static bool variable_length_or(const T ... v) {
  return (v || ...);
}

bool foo () {
   return variable_length_or(true, false, true, false);
}

我觉得奇怪的是g ++和clang ++似乎都很好使用 -std = c ++ 14 编译器浏览器)。它们确实会产生警告:

what I find odd is that both g++ and clang++ seem to be fine with it when building with -std=c++14 (compiler-explorer). They do create a warning:

<source>:2:16: warning: pack fold expression is a C++17 extension [-Wc++17-extensions]

 return (v || ...);

这在某种程度上表明我正在写的东西在c ++ 17之前还不行,但是编译成功,并且代码似乎在执行应做的事情。我本来希望编译失败。

This somewhat indicates that what I'm writing is not okay before c++17, but the compilation succeeds and the code seems to do what it should. I would've expected the compilation to fail.

关于编译器为什么接受我的折叠表达式的任何解释?

Any explanation about why the compiler accepts my fold expression?

(应归功于贷项:我从问题中汲取了灵感,我可以检查是否所有 T bool 与建议的此处类似)

(credit where credit is due: I took inspiration from this question, and I could check if all T are bool similar to what is suggested here)

推荐答案

一个符合标准的C ++ 17编译器必须提供折叠表达式。但这是一项有用的语言功能,是否值得因为您以以前的语言模式进行编译而主动禁用

A conforming C++17 compiler has to provide fold expressions. But that's a useful language feature, is it worth actively disabling it just because you're compiling in a previous language mode?

允许实现提供扩展,只要它们不会改变格式正确的程序的行为即可( [intro.compliance] / 8 )。 C ++ 17之前的折叠表达式就是这样的扩展-它们纯粹是加法运算。因此,在C ++ 14模式下,在允许和禁止折叠表达式之间进行实用程序权衡时,gcc和clang似乎都倾向于允许。

Implementations are allowed to provide extensions, provided that they do not alter the behavior of well-formed programs ([intro.compliance]/8). Fold expressions in pre-C++17 are just such an extension - they're purely additive. So as a question of utility tradeoff between allowing and disallowing fold expressions in C++14 mode, it seems that both gcc and clang decided to lean towards allowing.

当然,您不应该依赖-如果您要编写C ++ 17代码,则应使用C +进行编译+17。如果您需要依赖它的帮助,可以使用 -pedantic-errors

Of course, you shouldn't rely on this - if you want to write C++17 code, you should compile in C++17. If you want help relying on it, you can compile with -pedantic-errors:


只要基本标准(请参见 -Wpedantic )要求进行诊断,在某些情况下,在编译时存在未定义的行为,而在某些其他情况下,则不能阻止正在编译的程序根据标准有效。这不等于 -Werror = pedantic ,因为此选项启用了错误,而该选项未启用,反之亦然。

Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard. This is not equivalent to -Werror=pedantic, since there are errors enabled by this option and not enabled by the latter and vice versa.

这篇关于使用c ++ 14构建时可以使用pack fold表达式(c ++ 17扩展名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆