以自动返回类型作为参数的函数
Function with auto return type as parameter
这是
不允许的,因为它包含auto
:
void f(auto fp())
{
}
// error: 'auto' not allowed in function prototype
我们可以显式地使其成为函数指针并得到相同的错误:
void f(auto (*fp))
{
}
现在,如果我们给它一个尾随返回类型:
void f(auto fp() -> int)
{
}
现在没事了。这是合法的,编译器错误还是疏忽?
有趣的是,它不允许我将auto
函数指针作为参数。
// OK
void f(auto f(auto () -> int) -> int)
{
}
// Complain
void f(auto f(auto (*fp) -> int) -> int)
{
}
用于
自动类型扣除的auto
不同于用于指定尾随返回类型的auto
。
C++11 中的函数参数不支持自动类型推断auto
。
但是,C++14 及更高版本支持 lambda 函数参数的auto
。实质上,这会在 lambda 类型中创建模板化operator()
。
您的示例
void f( auto fp() -> int )
{}
使用尾随返回类型语法指定 C++03 语法函数
void f( int fp() )
{}
由于用作形式参数类型的函数类型的衰减,等效
于void f( int (*fp)() )
{}
使用函数指针参数。
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类