'auto' 函数原型中不允许使用 Clang

'auto' not allowed in function prototype with Clang

使用带有标志 std=c++1y 的 Clang 3.5、3.6 或 3.7,以下代码无法编译:

#include <iostream>
auto foo(auto bar) { return bar; }
int main() {
  std::cout << foo(5.0f) << std::endl;
}

给出的错误是:

error: 'auto' not allowed in function prototype

我在使用 g++ 4.9 时没有出现错误。 产生此错误是因为 Clang 尚未实现此功能,还是因为不允许我这样做而 GCC 以某种方式允许它?

您可以改用模板:

template<class A>
A foo(A bar) { return bar; }

仅当编译器可以从上下文中推断出类型时才允许使用自动。

编译器无法从上下文推断类型。

做错了什么

template<typename Y>
Y foo(Y bar){return bar;}

你必须按值传递 bar 吗?

在您的情况下,您可以使用 尾随 return 类型 语法:

auto foo(auto bar) -> decltype(bar)

正如我们从 ISO C++ 讨论邮件中看到的那样:decltype(auto) parameters vs. perfect forwarding auto parameters of non-lambdas is part of 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.

如果我们将 -pedantic 标志与 gcc 一起使用,我们会收到以下警告:

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

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

正如 dyp 指出的那样,polymorphic lambdas 确实进入了 C++14 并允许自动参数,一个例子来自论文:

// '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");

顺便说一句,这与您要在示例中实现的功能相同。

虽然您的特定语法没有进入 C++14,但一个类似的选项是:

static auto foo = [](auto bar) { return bar; };

这实现了基本相同的事情。