 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Javier Loureiro Guest
|
Posted: Sat Jun 10, 2006 4:35 am Post subject: templated constanst |
|
|
how can I write this template without a static member?
-- header --
template <typename Scalar>
struct A
{
static Scalar MAX_VALUE;
};
-- explict instance --
A<float>::MAX_VALUE = FLT_MAX;
A<double>::MAX_VALUE = DBL_MAX;
using it like:
double v = Math<double>::MAX_VALUE;
It´s the only static dependency for my algebra template class...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Jun 11, 2006 12:48 am Post subject: Re: templated constanst |
|
|
In article <1149872688.630467.262110 (AT) h76g2000cwa (DOT) googlegroups.com>,
Javier Loureiro <derethorspam (AT) gmail (DOT) com> wrote:
| Quote: | how can I write this template without a static member?
-- header --
template <typename Scalar
struct A
{
static Scalar MAX_VALUE;
};
-- explict instance --
A<float>::MAX_VALUE = FLT_MAX;
A<double>::MAX_VALUE = DBL_MAX;
using it like:
double v = Math<double>::MAX_VALUE;
It´s the only static dependency for my algebra template class...
static member functions ok?? they can be written and not require |
separate initialization.
why not use std::numeric_limits<type> or its approach to provide
functions to provide the results as they are not compile time constants
template <class T> class A;
template <> class A<float> {static float max() {return FLT_MAX;}};
template <> class A<double> {static double max() const {return
DBL_MAX;}};
//etc.
// usage: double v = A<double>::max();
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Sun Jun 11, 2006 12:49 am Post subject: Re: templated constanst |
|
|
Javier Loureiro wrote:
| Quote: | how can I write this template without a static member?
|
You can't.
| Quote: |
-- header --
template <typename Scalar
struct A
{
static Scalar MAX_VALUE;
};
-- explict instance --
A<float>::MAX_VALUE = FLT_MAX;
A<double>::MAX_VALUE = DBL_MAX;
|
This is a common approach, such classes are usually called "traits".
In this particular case there is already one
#include <limits>
double max = std::numeric_limits<double>::max();
--
Valentin Samko - http://www.valentinsamko.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Thu Jun 15, 2006 8:02 pm Post subject: Re: templated constanst |
|
|
Javier Loureiro wrote:
| Quote: | I just read the "limits" header (included with visual 2003),
and it uses the macro :
#define _STCONS(ty, name, val) static const ty name = (ty)(val)
to declare numeric_limits member variables like has_infinity,
is_integer, etc, inside a struct declaration...
I wrote this simple code using such macro and the limits class
inheritance, and it does not compile...
#define MY_STCONS(ty, name, val) static const ty name = (ty)(val)
namespace mystd {
struct num_base
{
MY_STCONS(bool, is_bounded, false);
MY_STCONS(bool, is_exact, false);
};
template <class T
struct num_procs : public num_base
{
inline static foo() { return T(0); }
};
struct num_int_base : public num_base
{
MY_STCONS(bool, is_bounded, false);
MY_STCONS(bool, is_exact, false);
};
template <> struct num_procs<int> : public num_int_base
{
typedef int T;
inline static foo() { return T(20); }
};
|
What's the error message? I see an obvious error in the fact
that you don't declare a return type for the functions foo(),
but that error should be clear from the error message. The rest
seems OK at first glance.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|