Re: How to work around 14.5.5, 14.7.3/18 and related?
Stephen Waits wrote:
[]
[color=blue]
> This working example shows basically what I want to do - only in a
> global scope.
>
> template <unsigned int I>
> float Iterative(float x)
> {
> return x + Iterative<I-1>(x);
> }
>
> template <>
> float Iterative<0>(fl oat x)
> {
> // specialize 0th iteration to stop template recursion
> return 0.0f;
> }
>
> However, when I decide to put this into a class and template out
> "float" (similar to the Math<T> class above) I run into problems with
> the language; for example:
>
> [Begin BROKEN example
>
> template <class T>
> class SomeClass
> {
> public:
>
> template <unsigned int I>
> static T Iterative(T x);
> };
>
> template <class T>
> template <unsigned int I> // not allowed!
> T SomeClass<T>::I terative<I>(T x)
> {
> }
>
> --end BROKEN example]
>
> Because this is a static member I cannot specialize it (because I
> believe this is a function template, of which specialization is NOT
> allowed by C++). Correct me if I'm wrong.
>[/color]
To fix it throw out <I>
template <class T>
template <unsigned int I>
T SomeClass<T>::I terative(T x)
{
}
[color=blue]
> So, my next step is attempting to put this in a nested functor:
>
> [Begin BROKEN example
>
> template <class T>
> class SomeClass
> {
> public:
> template <unsigned int I>
> class Iterative
> {
> public:
> static T IterativeImpl(T x);
> };
> };
>
>
> template <class T>
> template <unsigned int I>
> T SomeClass<T>::I terative<I>::It erativeImpl(T x)
> {
> }
>
> template <class T>
> template <> // not allowed!
> T SomeClass<T>::I terative<0>::It erativeImpl(T x)
> {
> }
>
> --end BROKEN example]
>
> And, of course, as you experts know already, specialization of this
> nested class is not allowed because the enclosing class template is
> not specialized. Again, correct me if I'm wrong here.
>
> What do you suggest I do to work around these issues?[/color]
Use overloading instead of specialization:
template<unsign ed n>
struct unsigned_ // resembles boost::mpl::int _<>
{
enum { value = n };
};
template<class T>
struct some
{
template<unsign ed n>
static void do_some();
template<unsign ed n>
static void do_some_imp(uns igned_<n>);
Re: How to work around 14.5.5, 14.7.3/18 and related?
Stephen Waits wrote:
[]
[color=blue]
> This working example shows basically what I want to do - only in a
> global scope.
>
> template <unsigned int I>
> float Iterative(float x)
> {
> return x + Iterative<I-1>(x);
> }
>
> template <>
> float Iterative<0>(fl oat x)
> {
> // specialize 0th iteration to stop template recursion
> return 0.0f;
> }
>
> However, when I decide to put this into a class and template out
> "float" (similar to the Math<T> class above) I run into problems with
> the language; for example:
>
> [Begin BROKEN example
>
> template <class T>
> class SomeClass
> {
> public:
>
> template <unsigned int I>
> static T Iterative(T x);
> };
>
> template <class T>
> template <unsigned int I> // not allowed!
> T SomeClass<T>::I terative<I>(T x)
> {
> }
>
> --end BROKEN example]
>
> Because this is a static member I cannot specialize it (because I
> believe this is a function template, of which specialization is NOT
> allowed by C++). Correct me if I'm wrong.
>[/color]
To fix it throw out <I>
template <class T>
template <unsigned int I>
T SomeClass<T>::I terative(T x)
{
}
[color=blue]
> So, my next step is attempting to put this in a nested functor:
>
> [Begin BROKEN example
>
> template <class T>
> class SomeClass
> {
> public:
> template <unsigned int I>
> class Iterative
> {
> public:
> static T IterativeImpl(T x);
> };
> };
>
>
> template <class T>
> template <unsigned int I>
> T SomeClass<T>::I terative<I>::It erativeImpl(T x)
> {
> }
>
> template <class T>
> template <> // not allowed!
> T SomeClass<T>::I terative<0>::It erativeImpl(T x)
> {
> }
>
> --end BROKEN example]
>
> And, of course, as you experts know already, specialization of this
> nested class is not allowed because the enclosing class template is
> not specialized. Again, correct me if I'm wrong here.
>
> What do you suggest I do to work around these issues?[/color]
Use overloading instead of specialization:
template<unsign ed n>
struct unsigned_ // resembles boost::mpl::int _<>
{
enum { value = n };
};
template<class T>
struct some
{
template<unsign ed n>
static void do_some();
template<unsign ed n>
static void do_some_imp(uns igned_<n>);
Comment