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(cons t T x, const T guess)
{
return SqrtApprox<iter ations-1>( x, T(0.5f) * (guess + (x/guess)) );
}
template <>
T SqrtApprox<0>(c onst 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>(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.
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?
Thanks,
Steve
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ] 18 1152 st***@waits.net (Stephen Waits) wrote in
news:f3******** *************** ***@posting.goo gle.com: 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>::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?
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>::I nner<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.goo gle.com: 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>::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?
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>::I nner<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*****@qwerty uiop.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>::I nner<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*****@qwerty uiop.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>::I nner<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(cons t T x, const T guess) { return SqrtApprox<iter ations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(c onst 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(cons t T x, const T guess,
UnsignedInt<ite rations>* = 0)
{
return SqrtApprox(
x,
T(0.5f) * (guess + (x/guess)),
(UnsignedInt<it erations - 1>*)0
);
}
private:
static T SqrtApprox(cons t T x, const T guess, UnsignedInt<0>* )
{
return guess;
}
};
#include <iostream>
int main()
{
std::cout << Math<double>::S qrtApprox<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(cons t T x, const T guess) { return SqrtApprox<iter ations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(c onst 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(cons t T x, const T guess,
UnsignedInt<ite rations>* = 0)
{
return SqrtApprox(
x,
T(0.5f) * (guess + (x/guess)),
(UnsignedInt<it erations - 1>*)0
);
}
private:
static T SqrtApprox(cons t T x, const T guess, UnsignedInt<0>* )
{
return guess;
}
};
#include <iostream>
int main()
{
std::cout << Math<double>::S qrtApprox<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********@hot mail.com> wrote in
news:4t******** *************** *********@4ax.c om: 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>::I nner<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********@hot mail.com> wrote in
news:4t******** *************** *********@4ax.c om: 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>::I nner<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********@hot mail.com> wrote in
news:0j******** *************** *********@4ax.c om: On 7 Apr 2004 10:26:04 -0400, st***@waits.net (Stephen Waits) wrote:
Occasionall y 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(cons t T x, const T guess) { return SqrtApprox<iter ations-1>( x, T(0.5f) * (guess + (x/guess)) ); }
template <> T SqrtApprox<0>(c onst 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(cons t T x, const T guess, UnsignedInt<ite rations>* = 0) { return SqrtApprox( x, T(0.5f) * (guess + (x/guess)), (UnsignedInt<it erations - 1>*)0 ); }
private: static T SqrtApprox(cons t T x, const T guess, UnsignedInt<0>* ) { return guess; } };
#include <iostream>
int main() { std::cout << Math<double>::S qrtApprox<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(cons t T x, const T guess)
{ return SqrtApproxImpl< n>::eval(x, guess); }
};
#include <iostream>
int main()
{
std::cout << Math<double>::S qrtApprox<10>(9 .5, 3.1) << std::endl;
return 0;
} 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 example:
template <unsigned int iterations>
T SqrtApprox(const T x, const T guess)
{
return SqrtApprox<iterations-1>( x, T(0.5f) * (guess + (x/guess)) );
}
|
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 all obvious in VBA which lacks
inheritance. I'm trying the explanation again now.
I often find cases where a limited form of inheritance would eliminate
duplication my code that seems impossible to eliminate otherwise. I'm getting
very...
|
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 all obvious in VBA which lacks
inheritance. I'm trying the explanation again now.
I often find cases where a limited form of inheritance would eliminate
duplication my code that seems impossible to eliminate otherwise. I'm getting
very...
|
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 where surgeries.id = animals.id and only where the surgery
date was date_a or date_b.
I'm doing this in Microsoft Access 2000 and am tearing out my hair
|
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 working
in private-public partnerships on education policy. I'm one of the
principals.
Kirby
=====
| |
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |