我了解到自动推导的返回类型是c++14的一个特性,就像下面这样
template <typename Func, typename ...Args>
auto post(Func func, Args... args);
有人告诉我,如果我想在 C++11 中使用它,我必须使用尾随返回类型,就像这样
template <typename Func, typename ...Args>
auto post(Func func, Args... args) -> decltype(xxxxxx);
但实际上我可以在我的电脑上成功编译带有
std=c++11
的no-trailing-return-type类型,编译器不会报告错误,它只是报告这样的警告:
warning: 'post' function uses 'auto' type specifier without trailing return type [enabled by default]
尽管有此警告信息,该程序仍按照我的设计运行良好。为什么不是错误信息? 我想知道为什么编译器允许我使用
std=c++11
执行此操作,因为这是 c++14 功能。我注意到警告信息中有一个[enabled by default]
。这是否意味着编译器强制自己接受这种语法?顺便说一下,我的编译器是gcc 4.8.5
我用
std=c++11
(使用 gcc 4.8.5)编译这个
template <typename Func, typename ...Args>
auto post(Func func, Args... args);
我认为它应该报告一个错误,但它只是报告了一个警告,然后允许我这样做:
warning: 'post' function uses 'auto' type specifier without trailing return type [enabled by default]
因为编译器有C++14特性,以C++14模式编译。在 C++11 模式下,默认情况下,它会启用它,作为该语言的非标准扩展。将有一个选项可以关闭这些扩展,这将导致错误。