In-Class initialization of constant member
I know it's not possible to define a non-integer static const in C++ inside the definition of a class for obvious reasons of possible multiple instances of the same object through multiple inclusion of the same libraries. However, I would like to know if there is a way to work around that problem, because I have template that uses the type (int, float, double, quad, ...) as an argument and depending on that the constants should be of the same type (that has to do with allowing multiple precision computations). So putting these constants in a separate file (or at least outside of the class) is not really an option, because then I'd have to assign a type, which I don't want to do. Any suggestions?
|
Quote:
Code:
class Foo Code:
template <typename T> Anyhow, maybe I missed the issue you are having trouble with. |
I had left this thread but seeing as there has not been a correction:
Quote:
How about Code:
template<typename T> Code:
template<typename T> |
To get around that issue, the project I work on has frequently put function statics inside inline member functions.
Obviously, the syntax to access the "static" is different than if it were directly a class static, but the semantics are the same. Typically, the member function in which you wrap the static does nothing other than return a reference to that static. We have discovered several bugs in the Intel compiler (version 7 through 10) in compiling that construct and in several cases we needed to do something else because of compiler bugs when the code was correct. But those bugs were in fairly complicated examples and I expect you're using gcc, not intel as a compiler. I don't know of similar bugs in gcc. Code:
template<typename T> Technically it is more correct for the function to be class static as well, and when it matters we do that. |
Quote:
Code:
#include <iostream> ta0kira PS There are less trivial reasons to use this, but my example is just to show that it works. |
Quote:
I would also add that I do not think your code is valid either, it should have an out of class definition as well. Quote:
|
All times are GMT -5. The time now is 01:39 PM. |