-4

Possible Duplicate:
How much is too much with C++0x auto keyword

I find using "auto" near critical points maybe cause some problems.

This is the example code:

#include <iostream>
#include <typeinfo>
#include <limits>

using std::cout;
using std::endl;
using std::numeric_limits;
using std::cerr;

int main() {
   auto i = 2147483647 /* numeric_limits<int>::max() */ ;
   cout << "The type of i is " << typeid(i).name() << endl;

   int count = 0;
   for (auto i = 2147483647; 
        i < 2147483657 /* numeric_limits<int>::max() + 10 */ ; ++i) {
       cout << "i = " << i << " " << endl;

       if (count > 30) {
           cerr << "Too many loops." << endl;
           break;
       }
       ++count;
   }

   return 0;
}

The "auto" decides the type of "i" is integer, but the upper limit of integer is 2147483647, that's easily overflow.

That's the outputs on Ideone(gcc-4.5.1) and LWS(gcc-4.7.2). They're different: "i" remains 2147483647 in the loops on Ideone(gcc-4.5.1) and overflows on LWS(gcc-4.7.2). But none of them is the expecting result: 10 cycles, +1 every time.

Should I avoid to use "auto" near critical points? Or How I use "auto" appropriately here?

UPDATE: Someone says "Use auto everywhere you can." in this thread you tell me. I don't think that's quite right. Type "long long int" is more appropriate the type "int" here. I wonder where I can use "auto" safely, where can't.

UPDATE 2: The solution 4(b) of the article by Herb Sutter should have answered the question.

15
  • 1
    Welcome to undefined behavior?
    – user541686
    Commented Dec 29, 2012 at 12:57
  • 5
    How would your problem change if you had used int instead of auto? How is this related to type deduction? Commented Dec 29, 2012 at 13:00
  • 4
    Numeric literals are always int (even if they overflow). You need to use the correct suffix L or UL or LL or 'ULL` to correctly specify the type of the literal. Commented Dec 29, 2012 at 13:06
  • 1
    @UniversE: It does correctly deduce the type. The standard explicitly states that numeric literals are int (unless you add the appropriate suffix). It does not try and deduce the type of a literal based on the value. Commented Dec 29, 2012 at 13:11
  • 3
    @UniversE That’s irrelevant. The type is deduced correctly. I still don’t understand what the problem here is. The code is semantically incorrect but that is your fault, not the compiler’s, because you use the wrong type for your literals. Commented Dec 29, 2012 at 13:14

4 Answers 4

0

You should only rely on type deduction to work out the type of your variables if it's going to be correct. Here, the compiler makes the deduction that it's an int, which is right as far as the standard is concerned, but your specific problem requires another type with a larger range. When you use auto, you're saying "the compiler knows best", but the compiler doesn't always know everything.

You wouldn't use auto here, just as you wouldn't use int. You could make your literal have higher rank (stick L or LL after it - although they're not guaranteed to be any larger than your int) and then auto would deduce a larger integral type.

Not to mention that auto really saves you nothing in this case. auto is usually used to avoid typing long, ugly types or types that you don't know. In this case, the type is not long and ugly, and you do know it.

0

auto is just a syntactic sugar. It isn't a type, it just infers what type the right side is supposed to be and decides that variable's type by that.

If you give it literals, it will just infer the default type it is given by the compiler.
You just need to know what the actual type is.

3
  • I know it's not a type. I feel the most important point now is knowing where can safely use, where can't. Without that, "auto" is rather a trap than sugar.
    – UniversE
    Commented Dec 29, 2012 at 14:26
  • 1
    in c++ you always have to know the type. it just shortens the writing. if you don't know the type, figure it out, and only then use the auto. Commented Dec 29, 2012 at 22:30
  • Yochai Timmer, as Bjarne Stroustrup said, "The type of tmp should be what you get from multiplying a T by a U, but exactly what that is can be hard for the human reader to figure out, but of course the compiler knows once it has figured out what particular T and U it is dealing with."
    – UniversE
    Commented Dec 30, 2012 at 6:35
0

An numeric literal (without the decimal point) is always int unless you explicitly change its type.

int x = 2147483657; // 2147483657 is treated as an int.
                    // If it does not fit tough it will be truncated
                    // according to standard rules.

long x = 2147483657L; // The L suffix tells the compiler to treat it as a long.
                      // Here you will get the correct value assuming long is larger than int.

In your case:

for(auto i = 2147483647;i < 2147483657;) // is not going to work as i is always 
                                         // an int and literal overflows.

// Try correct types:

for(auto i = 2147483647L; i < 2147483657L;++i) //Now it should work correctly.
2
0

You are expecting too much out of auto. Your expectation is that auto will automatically deduce the type which is best for the manipulation that you are going to perform on your variable. This is semantic analysis and compilers are not expected to do that (most often, they cannot). They can't look forward into the way you are going to use the variable you declare later on in your program.

The auto keyword only saves you from the burden of explicitly writing on the left the type of the expression appearing on the right, avoiding possible redundancy and all problems connected with it (what if the type of the expression on the right changes?)

This said, all other answers are correct: if you want your variable i not to overflow, you should assign to it a long long literal (using the LL postfix).

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