且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Clang 的函数原型中不允许使用“自动"

更新时间:2023-11-09 20:23:40

正如我们从 ISO C++ 讨论邮件中看到的:decltype(auto) 参数与完美转发 非 lambda 的自动参数是 concepts lite 因此不在 C++14 中:

As we see from the ISO C++ discussion mailing: decltype(auto) parameters vs. perfect forwarding auto parameters of non-lambdas is part of concepts lite and therefore not in C++14:

clang 是正确的,因为我们还没有自动参数.Concepts lite 可能会带来这些,但 C++14 没有.

clang is correct in the sense that we don't yet have auto parameters. Concepts lite may bring those, but C++14 doesn't have them.

如果我们在 gcc 中使用 -pedantic 标志,我们会收到以下警告:

If we use the -pedantic flag with gcc we receive the following warning:

warning: ISO C++ forbids use of 'auto' in parameter declaration [-Wpedantic]
  auto foo(auto bar) { return bar; }
           ^

所以这看起来像是一个扩展.

So this looks like an extension.

正如 dyp 指出的那样,多态 lambdas 确实使它成为C++14 并且确实允许自动参数,示例取自论文:

As dyp pointed out, polymorphic lambdas did make it into C++14 and do allow auto parameters, an example taken from the paper:

// 'Identity' is a lambda that accepts an argument of any type and
// returns the value of its parameter.
auto Identity = [](auto a) { return a; };
int three = Identity(3);
char const* hello = Identity("hello");

顺便说一下,这与您希望在示例中实现的功能相同.

Which is incidentally the same functionality you want to implement in your example.