public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: ansis atteka <ansisatteka.dev@gmail.com>
To: gcc@gcc.gnu.org
Subject: Making a struct field constant, when this struct is imported to a  particular Cpp files
Date: Thu, 12 Nov 2009 13:45:00 -0000	[thread overview]
Message-ID: <26318895.post@talk.nabble.com> (raw)


Greetings,

I have a struct in a header file. And I would like to have some of this
struct member fields to be constant, in case if this header file is included
from some particular Cpp files. Motivation for this is to avoid some silly
programmer errors by accidentally writing to this variable, when developer
is not supposed to do that. I guess that it is possible to catch these
errors already at the compile time with a const keyword.

I already have approach, but I am not quite sure whether it is
safe(especially regarding the compiler optimizations). Below are uplaoded
files.

In this case util.cpp is not allowed to edit S.y contents, while rw.cpp is
allowed to do that. File h.hpp contains the struct which is customized at
the preprocessor execution time by looking at the READONLY define.

If this is not safe, are there any other approaches? I am afraid that
compiler might optimize out something. Assumption is that other threads will
not touch this member. 

Regards,
Ansis

http://old.nabble.com/file/p26318895/Makefile Makefile 
http://old.nabble.com/file/p26318895/rw.cpp rw.cpp 
http://old.nabble.com/file/p26318895/h.hpp h.hpp 
http://old.nabble.com/file/p26318895/util.cpp util.cpp 
http://old.nabble.com/file/p26318895/util.hpp util.hpp 


-- 
View this message in context: http://old.nabble.com/Making-a-struct-field-constant%2C-when-this-struct-is-imported-to-a-particular-Cpp-files-tp26318895p26318895.html
Sent from the gcc - Dev mailing list archive at Nabble.com.

             reply	other threads:[~2009-11-12 13:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-12 13:45 ansis atteka [this message]
2009-11-12 14:03 ` Axel Freyn
2009-11-12 21:16   ` Dave Korn

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=26318895.post@talk.nabble.com \
    --to=ansisatteka.dev@gmail.com \
    --cc=gcc@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).