Click here to Skip to main content
15,888,733 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: create data base in vc++6 Pin
_AnsHUMAN_ 4-Aug-08 20:00
_AnsHUMAN_ 4-Aug-08 20:00 
GeneralRe: create data base in vc++6 Pin
ani_ikram4-Aug-08 20:56
ani_ikram4-Aug-08 20:56 
GeneralRe: create data base in vc++6 Pin
_AnsHUMAN_ 4-Aug-08 21:46
_AnsHUMAN_ 4-Aug-08 21:46 
QuestionAn exception occurred while trying to run "Shell32.dll,Control_RunDLL ConnectorCpl.cpl Pin
monsieur_jj4-Aug-08 17:23
monsieur_jj4-Aug-08 17:23 
QuestionLINK : fatal error LNK1104: Pin
ani_ikram4-Aug-08 17:23
ani_ikram4-Aug-08 17:23 
AnswerRe: LINK : fatal error LNK1104: Pin
sudhir_Kumar4-Aug-08 19:20
sudhir_Kumar4-Aug-08 19:20 
QuestionClient rectangle not actualized after menu removal Pin
FloatingMarc4-Aug-08 16:40
FloatingMarc4-Aug-08 16:40 
QuestionInlining of consts Pin
not_in_use4-Aug-08 12:17
not_in_use4-Aug-08 12:17 
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 use myFile 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); } \
	} NAME


which 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
AnswerRe: Inlining of consts Pin
sashoalm4-Aug-08 21:53
sashoalm4-Aug-08 21:53 
GeneralRe: Inlining of consts Pin
not_in_use5-Aug-08 7:54
not_in_use5-Aug-08 7:54 
QuestionIFilter index Pin
tustin4-Aug-08 11:31
tustin4-Aug-08 11:31 
QuestionConcatenating Icons on status bar Pin
daves444-Aug-08 9:41
daves444-Aug-08 9:41 
QuestionWriting to a bitmap Pin
Anthony Appleyard4-Aug-08 9:31
Anthony Appleyard4-Aug-08 9:31 
AnswerRe: Writing to a bitmap Pin
Perspx4-Aug-08 9:36
Perspx4-Aug-08 9:36 
GeneralRe: Writing to a bitmap Pin
Anthony Appleyard4-Aug-08 9:52
Anthony Appleyard4-Aug-08 9:52 
GeneralRe: Writing to a bitmap Pin
Perspx4-Aug-08 9:57
Perspx4-Aug-08 9:57 
QuestionRe: Writing to a bitmap Pin
David Crow4-Aug-08 16:36
David Crow4-Aug-08 16:36 
QuestionHow to get file size? Pin
__DanC__4-Aug-08 7:39
__DanC__4-Aug-08 7:39 
AnswerRe: How to get file size? Pin
vikas amin4-Aug-08 8:08
vikas amin4-Aug-08 8:08 
AnswerRe: How to get file size? Pin
Mark Salsbery4-Aug-08 9:08
Mark Salsbery4-Aug-08 9:08 
QuestionCreateBitmapIndirect woes Pin
Anthony Appleyard4-Aug-08 6:34
Anthony Appleyard4-Aug-08 6:34 
AnswerRe: CreateBitmapIndirect woes Pin
Mark Salsbery4-Aug-08 7:37
Mark Salsbery4-Aug-08 7:37 
QuestionDoubt regarding server application Pin
sunny_vc4-Aug-08 3:13
sunny_vc4-Aug-08 3:13 
GeneralRe: Doubt regarding server application Pin
Perspx4-Aug-08 3:15
Perspx4-Aug-08 3:15 
QuestionRe: Doubt regarding server application Pin
led mike4-Aug-08 4:27
led mike4-Aug-08 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.