Output type inference in C ++ 14

I just read about a new function called "return type deduction", available in C ++ 14, and I have a question about recursion in a function of this type. I found out that the first return in this function should allow the return of the return type.

The examples provided by Wiki fully comply with this rule.

auto Correct(int i) { if (i == 1) return i; // return type deduced as int else return Correct(i-1)+i; // ok to call it now } auto Wrong(int i) { if (i != 1) return Wrong(i-1)+i; // Too soon to call this. No prior return statement. else return i; // return type deduced as int } 

My question is: Why, when I changed Wrong(int i) to Wrong(auto i) , did the Wrong(int i) function start to compile? What is hiding behind this small change?

+6
source share
1 answer

I believe that this is a mistake in the GCC implementation of its extension to C ++ 14 auto . Here is a program that seems to be designed to work:

 auto f(auto i) { return ""; } int main() { const char *s = f(1); return 0; } 

It does not work, it fails with an error: the conversion from 'int to' const char * "is incorrect, because GCC for some reason determines that the return type must be the same as the parameter type.

The same error can make code that should be rejected, for example, what in your question, compile without problems.

Of course, this error does not affect compliance, because no valid C ++ 14 program can use auto parameters outside of lambdas.

Reportedly, a week ago, GCC developers reported bug # 64969 , resulting in another SO question about this .

+6
source

Source: https://habr.com/ru/post/982429/


All Articles