1

I know that meaning of auto keyword has been changed completely from C++11. But Recently I wrote a following simple program that compiles & runs fine when compiling with =std=c++98 option.

#include <iostream>
void fun(auto int a)
{   
    a=3;
    std::cout<<a<<'\n';
}
int main()
{
    fun(3);
}

Orwell Dev C++ IDE gives me warning like following:

[Warning] 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]

So, is it fine to use auto for function parameters or should I never use auto like this as in above program to maintain compatibility with C++11?

3
  • 5
    Another question is "Why would you ever use auto there, in any version of C++?" I can't think of a reason. Jun 9, 2015 at 11:55
  • what does auto int should even mean?
    – David Haim
    Jun 9, 2015 at 11:56
  • 2
    The only reason I can think of is if you have code where you specifically need to issue an error in C++11 mode if one of the silent changes between C++98/C++03 and C++11 breaks the code. However, in that case, an #if condition with an #error if C++11 is used would be far better for readability.
    – user743382
    Jun 9, 2015 at 11:58

3 Answers 3

6

Until C++ 11 the auto keyword was a "storage class specifier" whereas with C++ 11 it becomes a type-induction specifier.

To answer your question: depending on the C++ standard you use to compile your code, adjust the use of the auto keyword accordingly. It's not portable across the pre/post C++ 11 boundary of the C++ standard.

1
  • Um, prior to C++11, auto as a storage class specifier. But it was also a storage class specifier that meant nothing different than omitting it... Jun 9, 2015 at 13:22
4

So, is it fine to use auto for function parameters or should I never use auto like this as in above program to maintain compatibility with C++11?

That depends on what you mean by "fine":

If you mean "will it compile?" then YES.

If you mean "is it a good practice", the answer is that it is not a practice at all; It was possible to do so and the code was perfectly valid (before C++11) but that time is passed, and I do not know of anyone who did this (not even for tricky interview questions).

In conclusion, don't do it.

0

auto prior to C++11 was a storage class specifier, like register or static or extern.

It, however, was the default storage class specifier. Valid C++03 code with it removed would have the same meaning, which is why C++11 felt comfortable stealing the keyword.

In short:

void fun1(auto int a) {   
  std::cout<<a<<'\n';
}
void fun2(int a) {   
  std::cout<<a<<'\n';
}

have the same meaning in C++03. In C++11, fun1 is ill-formed.

Simply remove it from all of your pre-C++11 codebases. If the code was valid C++03, it will continue to have the same meaning.

There is a (very small) problem that some compilers might implement the K&R era C "by default, a type is int". Ie, they might consider auto x; to mean auto int x;. This was not valid C++03, however. Compiling with sufficiently strict flags in C++03 mode should generate errors around that (ab)use of auto.

As an aside, C++11 also introduces a new storage class specifier, thread_local. It steals auto for the use of auto-typed variables, and auto is no longer a storage class specifier.

Not the answer you're looking for? Browse other questions tagged or ask your own question.