Search code examples
c++auto

why can I use deduced return type without trailing return type in c++11?


I learned that auto deduced return type is a c++14 feature, just like below

template <typename Func, typename ...Args>
auto post(Func func, Args... args);

and I was told that if I want to use this in C++11, I must use trailing return type, like this

template <typename Func, typename ...Args>
auto post(Func func, Args... args) -> decltype(xxxxxx);

but actually I can compile the no-trailing-return-type one with std=c++11 successfully in my PC, the compiler doesn't report error, it just report a warning like this:

warning: 'post' function uses 'auto' type specifier without trailing return type [enabled by default]

despite of this warning info, the program works fine as I designed. Why isn't it an error message? I wonder why the compiler allow me to do this with std=c++11 , since this is a c++14 feature. I notice there is a [enabled by default] in the warning info. Does it mean the compiler forces itself to accept this syntax ? By the way, my compiler is gcc 4.8.5

I compile this with std=c++11(using gcc 4.8.5)

template <typename Func, typename ...Args>
auto post(Func func, Args... args);

I think it should report an error, but it just did report a warning and then allow me to do that:

warning: 'post' function uses 'auto' type specifier without trailing return type [enabled by default]

Solution

  • Because the compiler has the C++14 feature, to compile in C++14 mode. In C++11 mode, by default, it enables it, as a non-standard extension to the language. The option -pedantic-errors turns off those extensions, which will make it an error.

    See it on godbolt