Topic: Ternary operator and forcing a reference to static const class member


Author: Alex Botezatu <abotezatu@gmail.com>
Date: Mon, 7 Sep 2009 09:07:21 CST
Raw View
On Nov 28 2007, 8:56 pm, James Kanze <james.ka...@gmail.com> wrote:
> > Vincent Rivi   re wrote:
> > > ...
> > > With GCC 3.4.4 and GCC 4.2.2, if fails at link time :
> > > $ g++ tst.cpp -o tst
> > > /tmp/ccym93Sz.o:tst.cpp:(.text+0x33): undefined reference to `A::C'
> > > collect2: ld returned 1 exit status

> > GCC is complaining correctly. In this case the static constant
> > member object is 'used' and, therefore, has to be defined.
>

Hi, James,

I came upon this post in my search for the reasons the ternary
operator uses implicit reference in Gcc 4.x.

I have code like above but with the following caveat:

int value = othervalue ? CONST1 : CONST2;

here, CONST1 and CONST2 are static const int members of the class. In
Gcc4+, this implicitly uses references to CONST1 and CONST2, forcing
me to use space for a constant when I would rather not. My question
is, is this legal C++? Or is it an optimization bug in GCC 4x? How
difficult would it be for the optimizer not to use references for
STATIC CONSTANT primitives in ternary operator?

While I understand that 3-way operator may be used as an lvalue, this
is not the case for constants. So, there is no reason why the compiler
would need a reference to a static constant integer, for example.

So far I had the idea of skirting initialization for static const
ints, to save space, in what I perceived static const ints to be the
equivalent of #defines, but better. In light of the ? operator
treatment in GCC4.x, this forces me to reconsider the idea. Can you
suggest an alternative?

Thank you.
Alex.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Tue, 8 Sep 2009 00:54:34 CST
Raw View
On 7 Sep., 17:07, Alex Botezatu <abotez...@gmail.com> wrote:
> On Nov 28 2007, 8:56 pm, James Kanze <james.ka...@gmail.com> wrote:
>
> > > Vincent Rivi   re wrote:
> > > > ...
> > > > With GCC 3.4.4 and GCC 4.2.2, if fails at link time :
> > > > $ g++ tst.cpp -o tst
> > > > /tmp/ccym93Sz.o:tst.cpp:(.text+0x33): undefined reference to `A::C'
> > > > collect2: ld returned 1 exit status
> > > GCC is complaining correctly. In this case the static constant
> > > member object is 'used' and, therefore, has to be defined.
>
> Hi, James,

I'm not James, but since this is a public newsgroup I decided
to reply anyway.

> I came upon this post in my search for the reasons the ternary
> operator uses implicit reference in Gcc 4.x.
>
> I have code like above but with the following caveat:
>
> int value = othervalue ? CONST1 : CONST2;
>
> here, CONST1 and CONST2 are static const int members of the class. In
> Gcc4+, this implicitly uses references to CONST1 and CONST2, forcing
> me to use space for a constant when I would rather not. My question
> is, is this legal C++? Or is it an optimization bug in GCC 4x?

This is not a compiler bug, but a "bug" in the standard, see:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#712
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#696

> How difficult would it be for the optimizer not to use references for
> STATIC CONSTANT primitives in ternary operator?

I don't think that it is a real problem for a compiler, but to be
fair,
I should add that I'm not a compiler writer ;-)

> While I understand that 3-way operator may be used as an lvalue, this
> is not the case for constants. So, there is no reason why the compiler
> would need a reference to a static constant integer, for example.

I agree.

> So far I had the idea of skirting initialization for static const
> ints, to save space, in what I perceived static const ints to be the
> equivalent of #defines, but better. In light of the ? operator
> treatment in GCC4.x, this forces me to reconsider the idea.

Usage of enumerations could be an alternative, but has - depen-
ding on your use-cases - the problem that they don't preserve
the type of the constant.

HTH & Greetings from Bremen,

Daniel Kr   gler




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]