Occasionally we want to perform a Sqrt where we don't need accuracy
and/or we have a good initial guess - a situation where an N-step Sqrt
series may be more optimal than a full on Sqrt(). For example:
template <unsigned int iterations>
T SqrtApprox(const T x, const T guess)
{
return SqrtApprox<iterations-1>( x, T(0.5f) * (guess + (x/guess)) );
}
template <>
T SqrtApprox<0>(const T x, const T guess)
{
return guess;
}
The problems came when I attempted to get this little template
metaprogram into our main Math<T> class which looks something like
this:
template <typename T>
class Math
{
public:
static T Sqrt(T x);
static T Sin(T x);
static T ASin(T x);
... // SqrtApprox should be in here
};
So, I've coded up a few simple examples describing the process I've
gone through in trying to get this to work...
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>(float 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>::Iterative<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.
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>::Iterative<I>::IterativeImpl(T x)
{
}
template <class T>
template <> // not allowed!
T SomeClass<T>::Iterative<0>::IterativeImpl(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?
Thanks,
Steve
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] 18 1136 st***@waits.net (Stephen Waits) wrote in
news:f3**************************@posting.google.c om: Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For example:
(...)
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>::Iterative<I>::IterativeImpl(T x) { }
template <class T> template <> // not allowed! T SomeClass<T>::Iterative<0>::IterativeImpl(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?
Actually I've been using this technique quite often. The difference was
though, that I've been putting the nested class definition inside the
outer class definition body.
The following works for me:
template <typename T>
struct Outer {
template <unsigned i>
struct Inner {
static T pow(T x)
{ return Inner<i-1>::pow(x)*x; }
};
template <>
struct Inner<1> {
static T pow(T x)
{ return x; }
};
template <>
struct Inner<0> {
static T pow(T x)
{ return 1; }
};
};
#include <iostream>
int main()
{
std::cout << Outer<float>::Inner<4>::pow(2) << std::endl;
return 0;
}
PS: Don't cross-post to a moderated group at the same time you're posting
here. You'll get more replies quicker. :) st***@waits.net (Stephen Waits) wrote in
news:f3**************************@posting.google.c om: Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For example:
(...)
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>::Iterative<I>::IterativeImpl(T x) { }
template <class T> template <> // not allowed! T SomeClass<T>::Iterative<0>::IterativeImpl(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?
Actually I've been using this technique quite often. The difference was
though, that I've been putting the nested class definition inside the
outer class definition body.
The following works for me:
template <typename T>
struct Outer {
template <unsigned i>
struct Inner {
static T pow(T x)
{ return Inner<i-1>::pow(x)*x; }
};
template <>
struct Inner<1> {
static T pow(T x)
{ return x; }
};
template <>
struct Inner<0> {
static T pow(T x)
{ return 1; }
};
};
#include <iostream>
int main()
{
std::cout << Outer<float>::Inner<4>::pow(2) << std::endl;
return 0;
}
PS: Don't cross-post to a moderated group at the same time you're posting
here. You'll get more replies quicker. :)
On Wed, 7 Apr 2004 15:05:46 +0000 (UTC), bartek
<ba*****@qwertyuiop.o2.pl> wrote: Actually I've been using this technique quite often. The difference was though, that I've been putting the nested class definition inside the outer class definition body.
But adding specializations there is illegal - specializations may only
appear at namespace scope.
The following works for me:
template <typename T> struct Outer { template <unsigned i> struct Inner { static T pow(T x) { return Inner<i-1>::pow(x)*x; } }; template <> struct Inner<1> { static T pow(T x) { return x; } }; template <> struct Inner<0> { static T pow(T x) { return 1; } }; };
#include <iostream>
int main() { std::cout << Outer<float>::Inner<4>::pow(2) << std::endl; return 0;
(Comeau C++ strict mode)
"main.cpp", line 9: error: explicit specialization is not allowed in
the current scope
template <>
^
Comeau compiles it in "Microsoft mode", and VC7.1 compiles it, so I
guess it is a Microsoft extension.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
On Wed, 7 Apr 2004 15:05:46 +0000 (UTC), bartek
<ba*****@qwertyuiop.o2.pl> wrote: Actually I've been using this technique quite often. The difference was though, that I've been putting the nested class definition inside the outer class definition body.
But adding specializations there is illegal - specializations may only
appear at namespace scope.
The following works for me:
template <typename T> struct Outer { template <unsigned i> struct Inner { static T pow(T x) { return Inner<i-1>::pow(x)*x; } }; template <> struct Inner<1> { static T pow(T x) { return x; } }; template <> struct Inner<0> { static T pow(T x) { return 1; } }; };
#include <iostream>
int main() { std::cout << Outer<float>::Inner<4>::pow(2) << std::endl; return 0;
(Comeau C++ strict mode)
"main.cpp", line 9: error: explicit specialization is not allowed in
the current scope
template <>
^
Comeau compiles it in "Microsoft mode", and VC7.1 compiles it, so I
guess it is a Microsoft extension.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
On 7 Apr 2004 10:26:04 -0400, st***@waits.net (Stephen Waits) wrote: Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For example:
template <unsigned int iterations> T SqrtApprox(const T x, const T guess) { return SqrtApprox<iterations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(const T x, const T guess) { return guess; }
The problems came when I attempted to get this little template metaprogram into our main Math<T> class which looks something like this:
template <typename T> class Math { public: static T Sqrt(T x); static T Sin(T x); static T ASin(T x); ... // SqrtApprox should be in here };
So, I've coded up a few simple examples describing the process I've gone through in trying to get this to work...
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.
IIRC you can specialize a member function template, as long as the
enclosing class is completely specialized.
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.
No, you're not wrong.
What do you suggest I do to work around these issues?
Overloading? Or you could rely on the optimizer to do a good job
passing the iteration as a normal parameter. Anyway, here's an
overloading based approach, which works around the problem without
needing to change the call syntax:
template <unsigned int N>
struct UnsignedInt
{
static unsigned int const value = N;
};
template <typename T>
class Math
{
public:
template <unsigned int iterations>
static T SqrtApprox(const T x, const T guess,
UnsignedInt<iterations>* = 0)
{
return SqrtApprox(
x,
T(0.5f) * (guess + (x/guess)),
(UnsignedInt<iterations - 1>*)0
);
}
private:
static T SqrtApprox(const T x, const T guess, UnsignedInt<0>*)
{
return guess;
}
};
#include <iostream>
int main()
{
std::cout << Math<double>::SqrtApprox<10>(9.5, 3.1) << '\n';
}
There may be other workarounds too.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
On 7 Apr 2004 10:26:04 -0400, st***@waits.net (Stephen Waits) wrote: Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For example:
template <unsigned int iterations> T SqrtApprox(const T x, const T guess) { return SqrtApprox<iterations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(const T x, const T guess) { return guess; }
The problems came when I attempted to get this little template metaprogram into our main Math<T> class which looks something like this:
template <typename T> class Math { public: static T Sqrt(T x); static T Sin(T x); static T ASin(T x); ... // SqrtApprox should be in here };
So, I've coded up a few simple examples describing the process I've gone through in trying to get this to work...
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.
IIRC you can specialize a member function template, as long as the
enclosing class is completely specialized.
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.
No, you're not wrong.
What do you suggest I do to work around these issues?
Overloading? Or you could rely on the optimizer to do a good job
passing the iteration as a normal parameter. Anyway, here's an
overloading based approach, which works around the problem without
needing to change the call syntax:
template <unsigned int N>
struct UnsignedInt
{
static unsigned int const value = N;
};
template <typename T>
class Math
{
public:
template <unsigned int iterations>
static T SqrtApprox(const T x, const T guess,
UnsignedInt<iterations>* = 0)
{
return SqrtApprox(
x,
T(0.5f) * (guess + (x/guess)),
(UnsignedInt<iterations - 1>*)0
);
}
private:
static T SqrtApprox(const T x, const T guess, UnsignedInt<0>*)
{
return guess;
}
};
#include <iostream>
int main()
{
std::cout << Math<double>::SqrtApprox<10>(9.5, 3.1) << '\n';
}
There may be other workarounds too.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
tom_usenet <to********@hotmail.com> wrote in
news:4t********************************@4ax.com: But adding specializations there is illegal - specializations may only appear at namespace scope.
The following works for me:
template <typename T> struct Outer { template <unsigned i> struct Inner { static T pow(T x) { return Inner<i-1>::pow(x)*x; } }; template <> struct Inner<1> { static T pow(T x) { return x; } }; template <> struct Inner<0> { static T pow(T x) { return 1; } }; };
#include <iostream>
int main() { std::cout << Outer<float>::Inner<4>::pow(2) << std::endl; return 0;
(Comeau C++ strict mode)
"main.cpp", line 9: error: explicit specialization is not allowed in the current scope template <> ^
Comeau compiles it in "Microsoft mode", and VC7.1 compiles it, so I guess it is a Microsoft extension.
OOPS...
Funny though - I'm using VC6. I don't quite get the point of such
extensions, considering how limited is VC6's support for templates,
except for opening ways for portability issues. sheesh.
Thanks for clearing this up.
Cheers!
tom_usenet <to********@hotmail.com> wrote in
news:4t********************************@4ax.com: But adding specializations there is illegal - specializations may only appear at namespace scope.
The following works for me:
template <typename T> struct Outer { template <unsigned i> struct Inner { static T pow(T x) { return Inner<i-1>::pow(x)*x; } }; template <> struct Inner<1> { static T pow(T x) { return x; } }; template <> struct Inner<0> { static T pow(T x) { return 1; } }; };
#include <iostream>
int main() { std::cout << Outer<float>::Inner<4>::pow(2) << std::endl; return 0;
(Comeau C++ strict mode)
"main.cpp", line 9: error: explicit specialization is not allowed in the current scope template <> ^
Comeau compiles it in "Microsoft mode", and VC7.1 compiles it, so I guess it is a Microsoft extension.
OOPS...
Funny though - I'm using VC6. I don't quite get the point of such
extensions, considering how limited is VC6's support for templates,
except for opening ways for portability issues. sheesh.
Thanks for clearing this up.
Cheers!
tom_usenet <to********@hotmail.com> wrote in
news:0j********************************@4ax.com: On 7 Apr 2004 10:26:04 -0400, st***@waits.net (Stephen Waits) wrote:
Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For example:
template <unsigned int iterations> T SqrtApprox(const T x, const T guess) { return SqrtApprox<iterations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(const T x, const T guess) { return guess; }
The problems came when I attempted to get this little template metaprogram into our main Math<T> class which looks something like this:
template <typename T> class Math { public: static T Sqrt(T x); static T Sin(T x); static T ASin(T x); ... // SqrtApprox should be in here };
So, I've coded up a few simple examples describing the process I've gone through in trying to get this to work...
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.
IIRC you can specialize a member function template, as long as the enclosing class is completely specialized.
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.
No, you're not wrong.
What do you suggest I do to work around these issues?
Overloading? Or you could rely on the optimizer to do a good job passing the iteration as a normal parameter. Anyway, here's an overloading based approach, which works around the problem without needing to change the call syntax:
template <unsigned int N> struct UnsignedInt { static unsigned int const value = N; };
template <typename T> class Math { public: template <unsigned int iterations> static T SqrtApprox(const T x, const T guess, UnsignedInt<iterations>* = 0) { return SqrtApprox( x, T(0.5f) * (guess + (x/guess)), (UnsignedInt<iterations - 1>*)0 ); }
private: static T SqrtApprox(const T x, const T guess, UnsignedInt<0>*) { return guess; } };
#include <iostream>
int main() { std::cout << Math<double>::SqrtApprox<10>(9.5, 3.1) << '\n'; }
There may be other workarounds too.
Indeed :) the following uses a separate template class to delegate work
into. Funny ... my compiler gives me an internal error with this. :)
template <unsigned n>
struct SqrtApproxImpl {
template <typename T>
static T eval(const T x, const T guess)
{return SqrtApproxImpl<n-1>::eval(x, T(0.5f) * (guess + (x/guess)));}
};
template <>
struct SqrtApproxImpl<0> {
template <typename T>
static T eval(const T x, const T guess)
{return guess;}
};
template <typename T>
struct Math {
template <unsigned n>
static T SqrtApprox(const T x, const T guess)
{ return SqrtApproxImpl<n>::eval(x, guess); }
};
#include <iostream>
int main()
{
std::cout << Math<double>::SqrtApprox<10>(9.5, 3.1) << std::endl;
return 0;
}
tom_usenet <to********@hotmail.com> wrote in
news:0j********************************@4ax.com: On 7 Apr 2004 10:26:04 -0400, st***@waits.net (Stephen Waits) wrote:
Occasionally we want to perform a Sqrt where we don't need accuracy and/or we have a good initial guess - a situation where an N-step Sqrt series may be more optimal than a full on Sqrt(). For example:
template <unsigned int iterations> T SqrtApprox(const T x, const T guess) { return SqrtApprox<iterations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(const T x, const T guess) { return guess; }
The problems came when I attempted to get this little template metaprogram into our main Math<T> class which looks something like this:
template <typename T> class Math { public: static T Sqrt(T x); static T Sin(T x); static T ASin(T x); ... // SqrtApprox should be in here };
So, I've coded up a few simple examples describing the process I've gone through in trying to get this to work...
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.
IIRC you can specialize a member function template, as long as the enclosing class is completely specialized.
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.
No, you're not wrong.
What do you suggest I do to work around these issues?
Overloading? Or you could rely on the optimizer to do a good job passing the iteration as a normal parameter. Anyway, here's an overloading based approach, which works around the problem without needing to change the call syntax:
template <unsigned int N> struct UnsignedInt { static unsigned int const value = N; };
template <typename T> class Math { public: template <unsigned int iterations> static T SqrtApprox(const T x, const T guess, UnsignedInt<iterations>* = 0) { return SqrtApprox( x, T(0.5f) * (guess + (x/guess)), (UnsignedInt<iterations - 1>*)0 ); }
private: static T SqrtApprox(const T x, const T guess, UnsignedInt<0>*) { return guess; } };
#include <iostream>
int main() { std::cout << Math<double>::SqrtApprox<10>(9.5, 3.1) << '\n'; }
There may be other workarounds too.
Indeed :) the following uses a separate template class to delegate work
into. Funny ... my compiler gives me an internal error with this. :)
template <unsigned n>
struct SqrtApproxImpl {
template <typename T>
static T eval(const T x, const T guess)
{return SqrtApproxImpl<n-1>::eval(x, T(0.5f) * (guess + (x/guess)));}
};
template <>
struct SqrtApproxImpl<0> {
template <typename T>
static T eval(const T x, const T guess)
{return guess;}
};
template <typename T>
struct Math {
template <unsigned n>
static T SqrtApprox(const T x, const T guess)
{ return SqrtApproxImpl<n>::eval(x, guess); }
};
#include <iostream>
int main()
{
std::cout << Math<double>::SqrtApprox<10>(9.5, 3.1) << std::endl;
return 0;
}
Stephen Waits wrote in news:f327e120.0404061034.73bffbf2
@posting.google.com: 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?
#include <iostream>
#include <ostream>
template <unsigned int I>
struct iterative_helper
{
template < typename Float >
static Float apply( Float x )
{
return x + iterative_helper<I-1>::apply( x );
}
};
template <>
struct iterative_helper< 0U >
{
template < typename Float >
static Float apply( Float x )
{
return Float();
}
};
template < typename T >
class SomeClass
{
public:
template <unsigned int I>
static T iterative( T x );
};
template < typename T >
template <unsigned int I>
T SomeClass< T >::iterative( T x )
{
return iterative_helper< I >::apply( x );
}
int main()
{
std::cout << SomeClass< float >::iterative< 32 >( 22 ) << std::endl;
}
Rob.
-- http://www.victim-prime.dsl.pipex.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Stephen Waits wrote in news:f327e120.0404061034.73bffbf2
@posting.google.com: 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?
#include <iostream>
#include <ostream>
template <unsigned int I>
struct iterative_helper
{
template < typename Float >
static Float apply( Float x )
{
return x + iterative_helper<I-1>::apply( x );
}
};
template <>
struct iterative_helper< 0U >
{
template < typename Float >
static Float apply( Float x )
{
return Float();
}
};
template < typename T >
class SomeClass
{
public:
template <unsigned int I>
static T iterative( T x );
};
template < typename T >
template <unsigned int I>
T SomeClass< T >::iterative( T x )
{
return iterative_helper< I >::apply( x );
}
int main()
{
std::cout << SomeClass< float >::iterative< 32 >( 22 ) << std::endl;
}
Rob.
-- http://www.victim-prime.dsl.pipex.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
bartek wrote:
[in-class template member specialisation] Funny though - I'm using VC6. I don't quite get the point of such extensions, considering how limited is VC6's support for templates, except for opening ways for portability issues.
I believe that VC6 allows in-class specialisation because it doesn't
handle out-of-class template member definitions. If that's true, VC7
probably is to be backward-compatible.
Martin
bartek wrote:
[in-class template member specialisation] Funny though - I'm using VC6. I don't quite get the point of such extensions, considering how limited is VC6's support for templates, except for opening ways for portability issues.
I believe that VC6 allows in-class specialisation because it doesn't
handle out-of-class template member definitions. If that's true, VC7
probably is to be backward-compatible.
Martin
Thanks to all. Helper idea looks like it should work great..
--Steve
Thanks to all. Helper idea looks like it should work great..
--Steve
Stephen Waits wrote:
[] 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>(float 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>::Iterative<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.
To fix it throw out <I>
template <class T>
template <unsigned int I>
T SomeClass<T>::Iterative(T x)
{
}
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>::Iterative<I>::IterativeImpl(T x) { }
template <class T> template <> // not allowed! T SomeClass<T>::Iterative<0>::IterativeImpl(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?
Use overloading instead of specialization:
template<unsigned n>
struct unsigned_ // resembles boost::mpl::int_<>
{
enum { value = n };
};
template<class T>
struct some
{
template<unsigned n>
static void do_some();
template<unsigned n>
static void do_some_imp(unsigned_<n>);
static void do_some_imp(unsigned_<0>);
};
template<class T>
template<unsigned n>
void some<T>::do_some() { return do_some_imp(unsigned_<n>()); }
template<class T>
template<unsigned n>
void some<T>::do_some_imp(unsigned_<n>) { return do_some_imp(unsigned_<n - 1>()); }
template<class T>
void some<T>::do_some_imp(unsigned_<0>) { ; }
int main()
{
some<int>::do_some<32>();
}
--
Maxim Yegorushkin
MetaCommunications Engineering http://www.meta-comm.com/engineering/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Stephen Waits wrote:
[] 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>(float 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>::Iterative<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.
To fix it throw out <I>
template <class T>
template <unsigned int I>
T SomeClass<T>::Iterative(T x)
{
}
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>::Iterative<I>::IterativeImpl(T x) { }
template <class T> template <> // not allowed! T SomeClass<T>::Iterative<0>::IterativeImpl(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?
Use overloading instead of specialization:
template<unsigned n>
struct unsigned_ // resembles boost::mpl::int_<>
{
enum { value = n };
};
template<class T>
struct some
{
template<unsigned n>
static void do_some();
template<unsigned n>
static void do_some_imp(unsigned_<n>);
static void do_some_imp(unsigned_<0>);
};
template<class T>
template<unsigned n>
void some<T>::do_some() { return do_some_imp(unsigned_<n>()); }
template<class T>
template<unsigned n>
void some<T>::do_some_imp(unsigned_<n>) { return do_some_imp(unsigned_<n - 1>()); }
template<class T>
void some<T>::do_some_imp(unsigned_<0>) { ; }
int main()
{
some<int>::do_some<32>();
}
--
Maxim Yegorushkin
MetaCommunications Engineering http://www.meta-comm.com/engineering/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stephen Waits |
last post by:
Occasionally we want to perform a Sqrt where we don't need accuracy
and/or we have a good initial guess - a situation where an N-step Sqrt
series may be more optimal than a full on Sqrt(). For...
|
by: Steve Jorgensen |
last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling
a particular case of implementation inheritance that would be easy and obvious
in a fully OOP language, but is not at...
|
by: Steve Jorgensen |
last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling
a particular case of implementation inheritance that would be easy and obvious
in a fully OOP language, but is not at...
|
by: Karen Hill |
last post by:
SELECT surgeries.*, animals.*
FROM surgeries INNER JOIN animals ON .=.
AND WHERE ((.=Date()) Or
.=Date());
I'm trying to write a query that joins two table together, animals and
surgeries...
|
by: kirby.urner |
last post by:
This archival (yet recent) posting to an obscure physics list might
be of some interest to those taking a longer view. The work/study
projects described below come from an Oregon think tank...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |