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 via email to