[cfe-dev] Compile-time ternary operator

David Blaikie dblaikie at gmail.com
Fri Jan 18 13:16:43 PST 2013


On Fri, Jan 18, 2013 at 8:29 AM, Anton Bachin <antonbachin at yahoo.com> wrote:
> Hello,
>
> I am making use of the ternary operator (?:) in a compile-time computation. An illustrative example:
>
> constexpr int array[] = {1};
> constexpr int value = true ? array[0] : array[-1];
>
> int main()
> {
>     return 0;
> }
>
> In the actual program, the condition determines whether it is safe to perform the access in the false-case.
>
> Clang fails to compile this program, complaining about the out-of-bounds access in the false-case. This is the case with clang++ 3.1 on BSD and OS X.

Could you show the exact clang output?

> GCC 4.7 compiles the program just fine.
>
> Is this a bug or the intent? If it's a bug, has it been fixed in 3.2?

ToT Clang still warns about the out of bounds access (which could be
considered reasonable, or not - since it is unreachable - perhaps that
should be under our "analysis based warnings" that don't fire unless
the code is reachable) but it doesn't fail to compile.



More information about the cfe-dev mailing list