How to work around 14.5.5, 14.7.3/18 and related?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Waits

    #16
    Re: How to work around 14.5.5, 14.7.3/18 and related?


    Thanks to all. Helper idea looks like it should work great..

    --Steve

    Comment

    • Stephen Waits

      #17
      Re: How to work around 14.5.5, 14.7.3/18 and related?


      Thanks to all. Helper idea looks like it should work great..

      --Steve

      Comment

      • Maxim Yegorushkin

        #18
        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>);

        static void do_some_imp(uns igned_<0>);
        };

        template<class T>
        template<unsign ed n>
        void some<T>::do_som e() { return do_some_imp(uns igned_<n>()); }

        template<class T>
        template<unsign ed n>
        void some<T>::do_som e_imp(unsigned_ <n>) { return do_some_imp(uns igned_<n - 1>()); }

        template<class T>
        void some<T>::do_som e_imp(unsigned_ <0>) { ; }

        int main()
        {
        some<int>::do_s ome<32>();
        }

        --
        Maxim Yegorushkin
        MetaCommunicati ons Engineering


        [ See http://www.gotw.ca/resources/clcm.htm for info about ]
        [ comp.lang.c++.m oderated. First time posters: Do this! ]

        Comment

        • Maxim Yegorushkin

          #19
          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>);

          static void do_some_imp(uns igned_<0>);
          };

          template<class T>
          template<unsign ed n>
          void some<T>::do_som e() { return do_some_imp(uns igned_<n>()); }

          template<class T>
          template<unsign ed n>
          void some<T>::do_som e_imp(unsigned_ <n>) { return do_some_imp(uns igned_<n - 1>()); }

          template<class T>
          void some<T>::do_som e_imp(unsigned_ <0>) { ; }

          int main()
          {
          some<int>::do_s ome<32>();
          }

          --
          Maxim Yegorushkin
          MetaCommunicati ons Engineering


          [ See http://www.gotw.ca/resources/clcm.htm for info about ]
          [ comp.lang.c++.m oderated. First time posters: Do this! ]

          Comment

          Working...