Is there a way I can define constant floats within class scope like you can with integral values? I was hoping boost could help in this area. For example: struct default_alpha { static const float value = 1.0f; }; The above is not legal, of course. Anyone? Thanks.
--- Robert Dailey wrote:
Is there a way I can define constant floats within class scope like you can with integral values? I was hoping boost could help in this area. For example:
struct default_alpha { static const float value = 1.0f; };
The above is not legal, of course. Anyone? Thanks.
Not yet. Work has been done to emulate compile-time doubles via MPL, but it's currently sitting in the vault: http://www.boost-consulting.com/vault/ Look inside the Template Metaprogramming folder for 'mpl_math.zip'. No docs yet, you'll have to rely on the example programs to get yourself acquainted. Cromwell D. Enage ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
Robert Dailey wrote:
Is there a way I can define constant floats within class scope like you can with integral values? I was hoping boost could help in this area. For example:
How do you intend to use the value? The following meets your criteria of class scope definition:
struct default_alpha { static const float value = 1.0f;
static float value(){ return 1.0f; }
};
The above is not legal, of course. Anyone? Thanks.
Jeff Flinn
Robert Dailey wrote:
Is there a way I can define constant floats within class scope like you can with integral values? I was hoping boost could help in this area. For example:
struct default_alpha { static const float value = 1.0f; };
The above is not legal, of course. Anyone? Thanks. In .h header:
struct default_alpha { static const float value; }; In .cc source file: const float default_alpha::value = 1.0f; Is this answer too simple? -- Dick Hadsell 914-259-6320 Fax: 914-259-6499 Reply-to: hadsell@blueskystudios.com Blue Sky Studios http://www.blueskystudios.com 44 South Broadway, White Plains, NY 10601
I'm going to be using this float to default-initialize a construction
parameter. For example:
struct default_alpha
{
static const float value = 1.0f;
};
class foo
{
foo( float r, float g, float b, float a = default_alpha::value );
};
I'm not 100% sure if the above is legal C++. I haven't gotten to the point
to where I could test it. I suppose I could try it with integrals, but I
just haven't gotten around to it.
On Dec 10, 2007 12:21 PM, Richard Hadsell
Robert Dailey wrote:
Is there a way I can define constant floats within class scope like you can with integral values? I was hoping boost could help in this area. For example:
struct default_alpha { static const float value = 1.0f; };
The above is not legal, of course. Anyone? Thanks.
In .h header:
struct default_alpha { static const float value; };
In .cc source file:
const float default_alpha::value = 1.0f;
Is this answer too simple?
-- Dick Hadsell 914-259-6320 Fax: 914-259-6499 Reply-to: hadsell@blueskystudios.com Blue Sky Studios http://www.blueskystudios.com 44 South Broadway, White Plains, NY 10601
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Apologies, let me elaborate: When I said "I'm not 100% sure if the above is
legal C++", I meant that I'm not sure if default-constructing parameter 'a'
the way I am in my previous snippet is legal (I know that the static const
float part is illegal)
On Dec 10, 2007 2:33 PM, Robert Dailey
I'm going to be using this float to default-initialize a construction parameter. For example:
struct default_alpha { static const float value = 1.0f; };
class foo { foo( float r, float g, float b, float a = default_alpha::value ); };
I'm not 100% sure if the above is legal C++. I haven't gotten to the point to where I could test it. I suppose I could try it with integrals, but I just haven't gotten around to it.
On Dec 10, 2007 12:21 PM, Richard Hadsell
wrote: Robert Dailey wrote:
Is there a way I can define constant floats within class scope like you can with integral values? I was hoping boost could help in this area. For example:
struct default_alpha { static const float value = 1.0f; };
The above is not legal, of course. Anyone? Thanks.
In .h header:
struct default_alpha { static const float value; };
In .cc source file:
const float default_alpha::value = 1.0f;
Is this answer too simple?
-- Dick Hadsell 914-259-6320 Fax: 914-259-6499 Reply-to: hadsell@blueskystudios.com Blue Sky Studios http://www.blueskystudios.com 44 South Broadway, White Plains, NY 10601
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Robert Dailey
I'm going to be using this float to default-initialize a construction parameter. For example: struct default_alpha { static const float value = 1.0f; }; class foo { foo( float r, float g, float b, float a = default_alpha::value ); };
You can use constant in namespace scope: namespace default_alpha { static const float value = 1.0f; } HTH, Roman Perepelitsa.
Well, the namespace idea won't work either. There's certain details I
omitted because I wasn't sure if they would be directly related to the
issue. However I'll go ahead and elaborate now.
I plan to specialize the struct containing the float to determine what
'value' I want. For example:
template< typename T >
struct default_alpha;
template<>
struct default_alpha<float>
{
static const float alpha = 1.0f;
};
template<>
struct default_alpha<unsigned char>
{
static const unsigned char = 255;
};
So, if I have a 'color' class that takes a template parameter, like below, I
can use the above specialized structures to figure out how to default
initialize the 'a' construction parameter.
template< typename T >
class Color
{
Color( T r, T g, T b, T a = default_alpha<T>::value );
};
Again, I wasn't sure if these new details were required from the very
beginning. Apologies for this. Thank you for everyone's continued help.
On Dec 11, 2007 1:14 AM, Roman Perepelitsa
Robert Dailey
writes: I'm going to be using this float to default-initialize a construction parameter. For example: struct default_alpha { static const float value = 1.0f; }; class foo { foo( float r, float g, float b, float a = default_alpha::value ); };
You can use constant in namespace scope:
namespace default_alpha { static const float value = 1.0f; }
HTH, Roman Perepelitsa.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Sorry, small corrections to my last code example (I had a few typos):
template< typename T >
struct default_alpha;
template<>
struct default_alpha<float>
{
static const float value = 1.0f; // Note that this is not legal. I'm
simply just presenting pseudo code.
};
template<>
struct default_alpha<unsigned char>
{
static const unsigned char value = 255;
};
template< typename T >
class Color
{
Color( T r, T g, T b, T a = default_alpha<T>::value );
};
On Dec 11, 2007 2:01 PM, Robert Dailey
Well, the namespace idea won't work either. There's certain details I omitted because I wasn't sure if they would be directly related to the issue. However I'll go ahead and elaborate now.
I plan to specialize the struct containing the float to determine what 'value' I want. For example:
template< typename T > struct default_alpha;
template<> struct default_alpha<float> { static const float alpha = 1.0f; };
template<> struct default_alpha<unsigned char> { static const unsigned char = 255; };
So, if I have a 'color' class that takes a template parameter, like below, I can use the above specialized structures to figure out how to default initialize the 'a' construction parameter.
template< typename T > class Color { Color( T r, T g, T b, T a = default_alpha<T>::value ); };
Again, I wasn't sure if these new details were required from the very beginning. Apologies for this. Thank you for everyone's continued help.
On Dec 11, 2007 1:14 AM, Roman Perepelitsa
wrote: Robert Dailey
writes: I'm going to be using this float to default-initialize a construction parameter. For example: struct default_alpha { static const float value = 1.0f; }; class foo { foo( float r, float g, float b, float a = default_alpha::value ); };
You can use constant in namespace scope:
namespace default_alpha { static const float value = 1.0f; }
HTH, Roman Perepelitsa.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Dec 11, 2007 1:03 PM, Robert Dailey
template< typename T > struct default_alpha;
template<> struct default_alpha<float> { static const float value = 1.0f; // Note that this is not legal. I'm simply just presenting pseudo code.
};
template<> struct default_alpha<unsigned char> { static const unsigned char value = 255; };
template< typename T > class Color { Color( T r, T g, T b, T a = default_alpha<T>::value ); };
So why not use Jeff Flinn's suggestion: template< typename T > struct default_alpha; template<> struct default_alpha<float> { static float value() { return 1.0f; } }; template<> struct default_alpha<unsigned char> { static unsigned char value() { return 255; } }; template< typename T > class Color { public: Color( T r, T g, T b, T a = default_alpha<T>::value() ); }; ...? Stjepan
Are function calls allowed to default initialize a construction parameter?
Does the compiler turn the function into a constant? How does that work? I
wasn't aware you could do this...
On Dec 11, 2007 8:02 PM, Stjepan Rajko
On Dec 11, 2007 1:03 PM, Robert Dailey
wrote: template< typename T > struct default_alpha;
template<> struct default_alpha<float> { static const float value = 1.0f; // Note that this is not legal. I'm simply just presenting pseudo code.
};
template<> struct default_alpha<unsigned char> { static const unsigned char value = 255; };
template< typename T > class Color { Color( T r, T g, T b, T a = default_alpha<T>::value ); };
So why not use Jeff Flinn's suggestion:
template< typename T > struct default_alpha;
template<> struct default_alpha<float> { static float value() { return 1.0f; } };
template<> struct default_alpha<unsigned char> { static unsigned char value() { return 255; } };
template< typename T > class Color { public: Color( T r, T g, T b, T a = default_alpha<T>::value() ); };
...?
Stjepan _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Dec 12, 2007 8:07 AM, Robert Dailey
Are function calls allowed to default initialize a construction parameter? Does the compiler turn the function into a constant? How does that work? I wasn't aware you could do this...
I was asking the same questions, which is what prompted me to try to find the answers in the standard. If you can get your hands on it (I'm looking at ISO/IEC 14882:2003), section 8.3.6. talks about what can and can't be used for default argument values. Anyway, the compiler will evaluate the expression when the function is called, but not all expressions are valid (the ones I put in the example should be). If this doesn't work for you, you can always have two constructors - one taking 4 arguments and one taking 3, initializing the alpha value to just about anything. I would suggest a C++ list or channel to get more info, we've strayed a bit from Boost :-) But I found it helpful too. Cheers, Stjepan
Well I read the standard, and it really more or less discusses what you
*can't* use to initialize default parameters. Amongst that list I didn't see
functions. And behold, it worked! I guess I don't need boost after all. All
these years I kept thinking that only compile-time constants could be used
to initialize default parameters. I never use default parameters anyway, so
I can understand. Just goes to show that you learn new things every day,
regardless of how obvious things are :)
Thanks guys.
On Dec 12, 2007 10:08 AM, Stjepan Rajko
Are function calls allowed to default initialize a construction
On Dec 12, 2007 8:07 AM, Robert Dailey
wrote: parameter? Does the compiler turn the function into a constant? How does that work? I wasn't aware you could do this...
I was asking the same questions, which is what prompted me to try to find the answers in the standard. If you can get your hands on it (I'm looking at ISO/IEC 14882:2003), section 8.3.6. talks about what can and can't be used for default argument values. Anyway, the compiler will evaluate the expression when the function is called, but not all expressions are valid (the ones I put in the example should be). If this doesn't work for you, you can always have two constructors - one taking 4 arguments and one taking 3, initializing the alpha value to just about anything.
I would suggest a C++ list or channel to get more info, we've strayed a bit from Boost :-) But I found it helpful too.
Cheers,
Stjepan _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (6)
-
Cromwell Enage
-
Jeff Flinn
-
Richard Hadsell
-
Robert Dailey
-
Roman Perepelitsa
-
Stjepan Rajko