[Bug c++/55737] New: Template and the constant, short-form if-then-else condition issue

greenscape777 at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Dec 19 00:08:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55737

             Bug #: 55737
           Summary: Template and the constant, short-form if-then-else
                    condition issue
    Classification: Unclassified
           Product: gcc
           Version: 4.6.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: greenscape777@gmail.com


Please part me if the summary isn't so informative. Can't put what's on my mind
in a few words.

So my system info is:
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Linux 3.2.0-34-generic-pae #53-Ubuntu SMP Thu Nov 15 11:11:12 UTC 2012 i686
athlon i386 GNU/Linux

Consider the next code snippet:

template <int X>
struct DivByX
{
    enum
    {
        value1 = X,
        value2 = int(100.f / value1)
    };
};

enum
{
    valueA = 0,
    /// False branch in the condition should never be taken into account.
    /// And moreover, template should be constructed.
    valueB = (/* Always false >>>*/1 > 2 /*<<<*/) ? /* False branch >>>*/((1 <
DivByX<0>::value2) ? 2 : 3)/*<<<*/ : 1
};


I did some template research and stumbled upon this annoying bug. It's really
annoying because i need to do stuff like:

template <int X>
struct DivByX
{
    enum
    {
        real_x = X == 0 ? 1 : X,
        value1 = X,
        value2 = int(100.f / value1)
    };
};

So the problem hides in the if-then-else branch, that is discarded because of
the condition, but in fact processed by the compiler. Though this happens only
when in the false branch expression there is template used. The error i get:

test.cpp: In instantiation of ‘DivByX<0>’:
test.cpp:63:89:   instantiated from here
test.cpp:52:5: warning: division by zero [-Wdiv-by-zero]
test.cpp:52:5: error: ‘(1.0e+2f / 0.0f)’ is not a constant expression
test.cpp:52:5: error: enumerator value for ‘value2’ is not an integer constant

To give some comparison: clang works as intended.

Thank you for your attention.



More information about the Gcc-bugs mailing list