etmc / tmLQCD

tmLQCD is a freely available software suite providing a set of tools to be used in lattice QCD simulations. This is mainly a HMC implementation (including PHMC and RHMC) for Wilson, Wilson Clover and Wilson twisted mass fermions and inverter for different versions of the Dirac operator. The code is fully parallelised and ships with optimisations for various modern architectures, such as commodity PC clusters and the Blue Gene family.
http://www.itkp.uni-bonn.de/~urbach/software.html
GNU General Public License v3.0
32 stars 47 forks source link

Passing constant pointers #210

Open deuzeman opened 11 years ago

deuzeman commented 11 years ago

Nothing very important, but my compiler whines about this. In recent code, I've been seeing the following syntax crop up repeatedly where const pointers are supposed to be passed:

const char const *git_hash

This is syntactically equivalent to

char const const *git_hash or const const char *git_hash

What is presumably intended is:

char const * const git_hash

Const is allowed on both sides of the type declaration for some reason, but that leads to ambiguities for pointers. In those cases, only a post-fix const qualifier will work. I propose we try to use the post-fix version everywhere to avoid confusion.

kostrzewa commented 11 years ago

What was intended was:

const char * const

which is a constant pointer to a constant object. This should also be equivalent to:

char const * const

By contrast,

double * const

which is what we usually use is a constant pointer to a possibly non-constant object. (therefore causing the compiler to reload said object at each dereference of the pointer, unless the compiler recognizes the constant-ness of the object through inspection)

see here for a very insightful listing: http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967/Constant-Pointers-and-Pointers-to-Constants.htm

kostrzewa commented 11 years ago

I introduced this, for instance, in expo:

void exposu3_check(su3* const vr, const su3adj* const p, int im)

where the momenta are passed as a constant pointer to a constant object.

deuzeman commented 11 years ago

I fully agree with the modification! The only problem was that the compiler complained about double constant declarations, because the code as written did not actually declare a constant pointer to constant data. Writing either const char * const or char const * const is completely equivalent, but we need to be careful not to confuse this with const char const *...

kostrzewa commented 11 years ago

I'm just wondering where the warning originated because write_first_messages has the correct pointer, no?

kostrzewa commented 11 years ago

Oh, I see, sorry that's my bad!

deuzeman commented 11 years ago

In fairness, this may actually just be in one function. I fixed this before somewhere, but it may well have been in the same function on a different branch. And anyway, it's just a warning. It's just that the Intel compiler is very vocal about it and kind of starts spamming the warning whenever the header is included. :)