Inlining of consts
-
Hi everyone, I have a question that may sound a bit strange: What's the most efficient and elegant way to define constants in C++? I'm in a situation where I have to define constants that might be used in code that needs to be as fast as possible (such as maybe image or sound processing for example). I know the standard way in C++ is like this:
// myFile.h
const float myConst;// myFile.cpp
const float myConst = 3.5;Based on my understanding, the initialization must be performed in
myFile.cpp
since only int constants can be initialized in the header. The problem is that this looks a lot like the constant won't be inlined but always read from memory (or processor cache of course) when it is accessed. What's worse is that client code will most likely usemyFile
as part of a DLL/SO, so even if there were the slightest chance that the linker were trained to perform some magic inlining when linking the binary, I'd still be out of luck. Apart from that I need a portable solution that works reliably on Windows, Linux, Mac OS X, and if possible also on SGI Irix, independent of the compiler and linker. The obvious solution is to just use#define myConst 3.5
, but I'd like to avoid that if possible (for obvious reasons). If I'm right about no inlining being performed, does anyone have any proven workaround for this? All I could come up with was maybe abusing inline functions roughly like so (not tested, just an idea):#define INLINE_CONST( TYPE, NAME, VALUE ) \
struct FastConst_T_##NAME { \
inline operator TYPE { return (VALUE); } \
} NAMEwhich then could be used like that:
INLINE_CONST( double, pi, 3.141592653589793238462 ); // Pi from my memory, don't copy without checking
inline double circleArea( radius ) {
return r * r * pi;
}But that's obviously not a very elegant solution. I'm really sorry for the lengthy post, but I can't believe I have to use such hacks to get C++ to inline my constant values. I already tried Google, but I didn't find anything useful. Thanks in advance, Peter
-
Hi everyone, I have a question that may sound a bit strange: What's the most efficient and elegant way to define constants in C++? I'm in a situation where I have to define constants that might be used in code that needs to be as fast as possible (such as maybe image or sound processing for example). I know the standard way in C++ is like this:
// myFile.h
const float myConst;// myFile.cpp
const float myConst = 3.5;Based on my understanding, the initialization must be performed in
myFile.cpp
since only int constants can be initialized in the header. The problem is that this looks a lot like the constant won't be inlined but always read from memory (or processor cache of course) when it is accessed. What's worse is that client code will most likely usemyFile
as part of a DLL/SO, so even if there were the slightest chance that the linker were trained to perform some magic inlining when linking the binary, I'd still be out of luck. Apart from that I need a portable solution that works reliably on Windows, Linux, Mac OS X, and if possible also on SGI Irix, independent of the compiler and linker. The obvious solution is to just use#define myConst 3.5
, but I'd like to avoid that if possible (for obvious reasons). If I'm right about no inlining being performed, does anyone have any proven workaround for this? All I could come up with was maybe abusing inline functions roughly like so (not tested, just an idea):#define INLINE_CONST( TYPE, NAME, VALUE ) \
struct FastConst_T_##NAME { \
inline operator TYPE { return (VALUE); } \
} NAMEwhich then could be used like that:
INLINE_CONST( double, pi, 3.141592653589793238462 ); // Pi from my memory, don't copy without checking
inline double circleArea( radius ) {
return r * r * pi;
}But that's obviously not a very elegant solution. I'm really sorry for the lengthy post, but I can't believe I have to use such hacks to get C++ to inline my constant values. I already tried Google, but I didn't find anything useful. Thanks in advance, Peter
If it weren't for the doubles, I'd say use enum. It is definitely inline, and it has scope. Otherwise, you could probably use an inline function:
double MyConstFunc() { return 3.5; }
It should be inline, and you write it inside the header. I don't know how it would work with dlls though.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal
-
If it weren't for the doubles, I'd say use enum. It is definitely inline, and it has scope. Otherwise, you could probably use an inline function:
double MyConstFunc() { return 3.5; }
It should be inline, and you write it inside the header. I don't know how it would work with dlls though.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal
Thanks for your reply. Inline functions are not a problem with DLLs since the implementation has to be inside the header files, which means that the source code is available and can be inlined by the compiler, so no problem there. The solution you are proposing is exactly what the macro from my post would do (the macro would just add the benefit that it is transparent to client code because it would provide the semantics of a regular const). But I found it hard to believe that C++ offers no built-in solution. I mean, the standard allows inlining of functions, but it doesn't seem to be able to inline constants. Well, I guess I'll just have to bite the bullet and live with it then. Thanks again, Peter