Clang 的函数原型中不允许使用“自动" [英] 'auto' not allowed in function prototype with Clang

查看:41
本文介绍了Clang 的函数原型中不允许使用“自动"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Clang 3.5, 3.6, or 3.7, with the flag std=c++1y the following code does not compile :

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

给出的错误是:

错误:函数原型中不允许使用'auto'

error: 'auto' not allowed in function prototype

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

I do not have errors using g++ 4.9. Is this error produced because Clang has not yet implemented this functionnality yet or is it because I am not allowed to do that and GCC somehow permits it ?

推荐答案

正如我们从 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.

这篇关于Clang 的函数原型中不允许使用“自动"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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