【发布时间】:2012-12-14 19:00:22
【问题描述】:
这两个函数有什么区别?
auto func(int a, int b) -> int;
int func(int a, int b);
【问题讨论】:
标签: c++ c++11 declaration function-declaration
这两个函数有什么区别?
auto func(int a, int b) -> int;
int func(int a, int b);
【问题讨论】:
标签: c++ c++11 declaration function-declaration
除了符号之外,上述情况没有任何区别。当您想要引用一个或多个参数来确定函数的返回类型时,替代函数声明语法变得很重要。例如:
template <typename S, typename T>
auto multiply(S const& s, T const& t) -> decltype(s * t);
(是的,这是一个愚蠢的例子)
【讨论】:
template <typename S, typename T> decltype(s * t) multiply(S const& s, T const& t);你知道为什么吗?
friend 函数一起使用,因为这些不能在类定义之外定义)。
这两个声明之间没有有用的区别;两个函数都返回一个int。
C++11 的尾随返回类型对于带有 template 参数的函数很有用,其中返回类型在编译时才知道,例如在这个问题中:How do I properly write trailing return type?
【讨论】:
它们使用不同的语法,并且只有一种在 C++11 之前的 C++ 版本中有效。除此之外,您在问题中显示的两个函数声明之间没有区别。
【讨论】: