1

I'm programming with C++ on Ubuntu 12.04 and g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3.

I have a templated Vector class

template <typename type, int length>
class Vector {
    // Implementation
};

which has length components of type type.

I use this class in some other class, which is also templated. This class takes the number of elements to use for its vectors as a template argument (dim).

This argument is either 2 or 3. I have to do special things, if dim == 3, so I wrote more or less often things like this:

if (dim == 3) {
    // do special things here
}

I thought, when the class is called with parameter 2, the compiler will see, that the condition is never true and will not translate it and vice versa, if the parameter is 3, the compiler will see, that the condition is never false and will translate everything and optimize out the if.

When compiling with -O0 I get no warnings, but when switching on -O3 I get the warning array subscript is above array bounds pointing to lines like this one

Vector<pr, dim> v;
v[0] = ...
v[1] = ...
if (dim == 3) v[2] = ...

where pr is a type, given as a parameter.

I don't understand, why I get the warnings, because: if dim == 2, the assignment will never be executed and if it's 3, the assignment does not cause any problems.

I thought all the time, templating will work like this: the compiler takes the template code and replaces all occurrences of parameters with the parameters given and then treats this, as it were a simple class.

My question now is, why is the compiler warning me about things, it knows, that they will never happen?

And why do I get the warnings only, when compiling with optimizations?

4
  • This question would be greatly improved with a concrete code example... Commented Dec 31, 2012 at 17:12
  • 1
    Warnings are not within the scope of the standard. Every compiler is free to implement whatever warnings they like, with whatever quality they like. Optimizations affect warnings because optimizations affect the codegen tree and the amount of flow control analysis performed. My guess is that the compiler cannot track the relationship between dim and the dimensionality of v so it doesn't realize that the two are linked. Commented Dec 31, 2012 at 17:13
  • I'm not completely sure about - because a lot of information is missing - but for me this looks that you need a template specialization for 2 and one for 3. Commented Dec 31, 2012 at 17:16
  • Providing code example is really difficult, because my program now is finished and ca. 2000 lines long. I noticed it now, because now I compiled with optimizations. I thought, the information provided would be enough, since the problem is only, that the compiler warns about a line within an if, where the condition is always false. I didn't believe, that g++ with -O3 translates such code. Commented Dec 31, 2012 at 17:38

1 Answer 1

2

The compiler compiles the code even when the if condition will never be fulfilled, and gives you the warning.

You can either ignore the warning because the if will prevent it from ever actually executing, or specialize/SFINAE the function/class to do the extra stuff when the parameter is 3.

Or wait ~5 years until we get static_if.

Sign up to request clarification or add additional context in comments.

2 Comments

I also thought, that the compiler translates it, even if the condition is always false, but I wondered, why it happens with optimizations on. I thought, especially then, the compiler factors such things out. But you brought me the idea: Specialization :)
@Wolfe the optimiser comes after the compiler; the compiler still has to check everything is correct (and issuing warnings is part of its job). This is why you can't do if (false) this_function_doesnt_exist(); and still get the code to compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.