Question

I have a large code and many of my functions accept arguments of the type
<typename> const * const <varname> or <typename> const * const * const <varname>

Would it be horribly abusive to define a preprocessor shorthand like
_CsC or _CsCsC

e.g.
#define _CsC const * const and #define _CsCsC const * const * const

Where the C stands for const and the s stands for * and the _ is to get your attention that this is a #defined shorthand.

I realize that the initial reaction might be "Huh what?" but once you've read more than a few lines of the code, seems like you'd adjust to it and it'd improve readability by allowing you to see the type/varname faster...

Initial tests compile and the double pointers are much more readable, it seems to me.

Était-ce utile?

La solution

Use a template:

template <typename T> using CsCsC = const * const * const T;

Usage:

CsCsC<Foo> x = /* ... */;

Autres conseils

It would be horrible and abusive, yes. Use a typedef instead.

To explain Luchian's answer:

if you use

typedef int * IntPtr;
typedef int ** IntPtrPtr;

and you write

const IntPtr x;  
const IntPtrPtr y;

then it is equal to

const int * const x;
const int * const * const y;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top