From: "John Ratliff" <webmaster@technoplaza.net>
To: "'MSX to GCC'" <gcc-help@gcc.gnu.org>
Subject: RE: C++ static integer class constants...
Date: Mon, 17 Oct 2005 20:04:00 -0000 [thread overview]
Message-ID: <20051017200400.teR4CtxXaUm6DDFDnKw1JbwVQPaOvhPtIJwZ5fX7CLo@z> (raw)
In-Reply-To: <2e393d080510171107l678b0570u8f9eb3876deac2a3@mail.gmail.com>
Okay. I no longer think g++ 3.3 is wrong according to the standard, but I
still think it is a bug. Not because it violates the standard, but because
it is inconsistent with itself.
Take the following example program:
#include <iostream>
class foo {
public:
static const unsigned int SRAM_SIZE = 0x2000;
static const int CHECKSUM_OFFSET = 0x8;
static const int CHECKSUM_OFFSET2 = 0x1FF8;
foo();
unsigned char getChecksum(bool redundant = false) const;
private:
char sram[SRAM_SIZE];
};
const int foo::CHECKSUM_OFFSET;
const int foo::CHECKSUM_OFFSET2;
foo::foo() {
sram[CHECKSUM_OFFSET] = 1;
sram[CHECKSUM_OFFSET2] = 2;
}
unsigned char foo::getChecksum(bool redundant) const {
int offset = (redundant ? CHECKSUM_OFFSET2 : CHECKSUM_OFFSET);
return sram[offset];
}
int main(int, char **) {
foo f;
unsigned char c1 = f.getChecksum();
unsigned char c2 = f.getChecksum(true);
std::cout << "c1 = " << static_cast<int>(c1) << '\n';
std::cout << "c2 = " << static_cast<int>(c2) << '\n';
return 0;
}
This program will is a conforming program according to the standard and
works just fine in g++ 3.3.
But, if you replace the getChecksum method with
return sram[redundant ? CHECKSUM_OFFSET2 : CHECKSUM_OFFSET];
then you can safely remove the namespace scope definition. It is not
technically a conforming program then according to the standard, but g++
optimizes it here to be a compile-time constant which didn't need external
linkage. The program would be wrong, but g++'s behavior with regard to
optimization is inconsistent with itself. If it's going to optimize, it
should optimize whether you store the constant in a temporary like offset
using the ternary operator or not. You can also replace getChecksum with
unsigned char foo::getChecksum(bool redundant) const {
int offset = CHECKSUM_OFFSET;
if (redundant) offset = CHECKSUM_OFFSET2;
return sram[offset];
}
and g++ will optimize it into a compile-time constant not requiring external
linkage. Something about the combination of the int offset and the ternary
operator assignment g++ 3.3 doesn't handle the way it handles everything
else.
Since the 3.3 series has been closed, I won't be filing a bug report, and
maybe the gcc team wouldn't even consider this a bug, but the inconsistency
is present, standards conformance notwithstanding.
--John Ratliff
next prev parent reply other threads:[~2005-10-17 20:04 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <200510160743.j9G7hVoa010304@inbound-smtp-1.corp.adobe.com>
2005-10-17 12:05 ` John Love-Jensen
2005-10-17 17:50 ` John Ratliff
2005-10-17 17:50 ` John Ratliff
[not found] ` <4353e481.0829b6d6.64bd.18a4SMTPIN_ADDED@mx.gmail.com>
2005-10-17 18:07 ` corey taylor
2005-10-17 20:04 ` John Ratliff
2005-10-17 20:04 ` John Ratliff [this message]
[not found] <0IOJ0024ULK8BF@emroute1.cind.ornl.gov>
2005-10-18 12:26 ` Ernest L. Williams Jr.
[not found] <87oe5pb5dp.fsf@totally-fudged-out-message-id>
2005-10-17 19:17 ` Florian Weimer
2005-10-18 6:19 ` John Ratliff
2005-10-18 6:19 ` John Ratliff
[not found] <20051016195314.7EFB824AEF5@lax-gw02.mroute.net>
2005-10-17 9:09 ` Nathan Sidwell
2005-10-17 19:21 ` Florian Weimer
[not found] <20051016200125.C061F13137C@arvo.suso.org>
2005-10-16 21:36 ` John Ratliff
2005-10-16 21:36 ` John Ratliff
2005-10-16 15:26 Ryan Mansfield
2005-10-16 16:16 ` John Ratliff
2005-10-16 16:16 ` John Ratliff
2005-10-16 16:42 ` John Ratliff
2005-10-16 16:42 ` John Ratliff
[not found] ` <43528310.03f34613.6a05.0e40SMTPIN_ADDED@mx.gmail.com>
2005-10-16 17:43 ` Alex J. Dam
[not found] ` <4352a3b2.42389977.3b5e.60fdSMTPIN_ADDED@mx.gmail.com>
2005-10-16 19:29 ` Alex J. Dam
2005-10-16 19:53 ` John Ratliff
2005-10-16 19:53 ` John Ratliff
2005-10-16 19:45 ` John Ratliff
2005-10-16 19:45 ` John Ratliff
[not found] <435202e6.3d759d8f.4550.fffff1e6SMTPIN_ADDED@mx.gmail.com>
2005-10-16 14:50 ` Alex J. Dam
2005-10-17 19:13 ` Florian Weimer
-- strict thread matches above, loose matches on Subject: below --
2005-10-16 7:35 John Ratliff
2005-10-16 7:35 John Ratliff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20051017200400.teR4CtxXaUm6DDFDnKw1JbwVQPaOvhPtIJwZ5fX7CLo@z \
--to=webmaster@technoplaza.net \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).