public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55737] New: Template and the constant, short-form if-then-else condition issue
@ 2012-12-19  0:08 greenscape777 at gmail dot com
  2012-12-19  0:24 ` [Bug c++/55737] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: greenscape777 at gmail dot com @ 2012-12-19  0:08 UTC (permalink / raw)
  To: gcc-bugs


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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-02-27 13:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-19  0:08 [Bug c++/55737] New: Template and the constant, short-form if-then-else condition issue greenscape777 at gmail dot com
2012-12-19  0:24 ` [Bug c++/55737] " pinskia at gcc dot gnu.org
2012-12-19 10:32 ` greenscape777 at gmail dot com
2012-12-19 15:54 ` redi at gcc dot gnu.org
2012-12-19 16:11 ` greenscape777 at gmail dot com
2012-12-19 16:13 ` redi at gcc dot gnu.org
2013-02-27 13:06 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).