'auto'不允许在Clang的函数原型 [英] 'auto' not allowed in function prototype with Clang

查看:1020
本文介绍了'auto'不允许在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)parameters versus perfect forwarding non-lambdas的自动参数是概念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是否正确感觉我们还没有汽车参数。

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 ,我们收到以下警告:

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.

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

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