Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it possible to specify template as <L/2>?

Reply
Thread Tools

Is it possible to specify template as <L/2>?

 
 
Atlas
Guest
Posts: n/a
 
      10-20-2005
I tried the following in MSVC7.1 but was told "unable to deduce
template parameter".
template <int L, int M, int T> class Quantity;
template <int L, int M, int T> Quantity<L/2,M/2,T/2> sqrt(const
Quantity<L,M,T>& q);
template <int L, int M, int T>
class Quantity
{public:...
friend Quantity<L/2,M/2,T/2> sqrt<L,M,T>(const Quantity<L,M,T>& q);
};
template <int L,int M, int T>
Quantity<L/2,M/2,T/2> sqrt(const Quantity<L,M,T>& q)
{
Quantity<L/2,M/2,T/2> q1;
return q1;
};
That's for a unit system operation.
I succeed with return type like <L1+L2,M1+M2,T1+T2> (For multiply).
Also for pow, I failed.

 
Reply With Quote
 
 
 
 
ben
Guest
Posts: n/a
 
      10-20-2005
Atlas wrote:
> I tried the following in MSVC7.1 but was told "unable to deduce
> template parameter".
> template <int L, int M, int T> class Quantity;
> template <int L, int M, int T> Quantity<L/2,M/2,T/2> sqrt(const
> Quantity<L,M,T>& q);
> template <int L, int M, int T>
> class Quantity
> {public:...
> friend Quantity<L/2,M/2,T/2> sqrt<L,M,T>(const Quantity<L,M,T>& q);
> };
> template <int L,int M, int T>
> Quantity<L/2,M/2,T/2> sqrt(const Quantity<L,M,T>& q)
> {
> Quantity<L/2,M/2,T/2> q1;
> return q1;
> };
> That's for a unit system operation.
> I succeed with return type like <L1+L2,M1+M2,T1+T2> (For multiply).
> Also for pow, I failed.
>


Compiles fine with VC 8

ben
 
Reply With Quote
 
 
 
 
Kristo
Guest
Posts: n/a
 
      10-20-2005
Atlas wrote:
> I tried the following in MSVC7.1 but was told "unable to deduce
> template parameter".
> template <int L, int M, int T> class Quantity;
> template <int L, int M, int T> Quantity<L/2,M/2,T/2> sqrt(const
> Quantity<L,M,T>& q);
> template <int L, int M, int T>
> class Quantity
> {public:...
> friend Quantity<L/2,M/2,T/2> sqrt<L,M,T>(const Quantity<L,M,T>& q);
> };
> template <int L,int M, int T>
> Quantity<L/2,M/2,T/2> sqrt(const Quantity<L,M,T>& q)
> {
> Quantity<L/2,M/2,T/2> q1;
> return q1;
> };
> That's for a unit system operation.
> I succeed with return type like <L1+L2,M1+M2,T1+T2> (For multiply).
> Also for pow, I failed.


L, M, and T in this case are constants, not integer variables. So the
compiler *should* be able to evaluate things like L/2. Apparently
yours does not, so the best solution is to get a better one.

FWIW, Comeau's online C++ compiler accepted the code after I removed
the extra semicolon and the ellipsis.

Kristo

 
Reply With Quote
 
Atlas
Guest
Posts: n/a
 
      10-21-2005

Kristo wrote:
> Atlas wrote:
> > I tried the following in MSVC7.1 but was told "unable to deduce
> > template parameter".
> > template <int L, int M, int T> class Quantity;
> > template <int L, int M, int T> Quantity<L/2,M/2,T/2> sqrt(const
> > Quantity<L,M,T>& q);
> > template <int L, int M, int T>
> > class Quantity
> > {public:...
> > friend Quantity<L/2,M/2,T/2> sqrt<L,M,T>(const Quantity<L,M,T>& q);
> > };
> > template <int L,int M, int T>
> > Quantity<L/2,M/2,T/2> sqrt(const Quantity<L,M,T>& q)
> > {
> > Quantity<L/2,M/2,T/2> q1;
> > return q1;
> > };
> > That's for a unit system operation.
> > I succeed with return type like <L1+L2,M1+M2,T1+T2> (For multiply).
> > Also for pow, I failed.

>
> L, M, and T in this case are constants, not integer variables. So the
> compiler *should* be able to evaluate things like L/2. Apparently
> yours does not, so the best solution is to get a better one.
>
> FWIW, Comeau's online C++ compiler accepted the code after I removed
> the extra semicolon and the ellipsis.
>
> Kristo


Thanks you all. To use VC2003, I modified my codes. Simply put,
eliminate the friend declaration. In this way it works, but I have to
implement a lot of seter/geter of my class members. The example codes
listed.
Maybe VC has some special requirements on template friend?

-------
template <int L, int M, int T>
class Quantity
{
public:

};

template <int L,int M, int T>
Quantity<L/2,M/2,T/2> sqrt(const Quantity<L,M,T>& q)
{
Quantity<L/2,M/2,T/2> q1;
return q1;

}

int main()
{
Quantity<0,0,2> q;
Quantity<0,0,1> w = sqrt(q);
return 0;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template function specialization inside template class possible? quarup@gmail.com C++ 7 08-25-2006 11:01 PM
About partially specify template member function PengYu.UT@gmail.com C++ 3 04-05-2006 10:40 AM
How to specify a template as the typename of a template PengYu.UT@gmail.com C++ 6 05-10-2005 01:17 PM
Is it possible to specify the size of list at construction? Anthony Liu Python 0 03-01-2005 08:33 PM
Template ctor - explicitly specify templ param Agoston Bejo C++ 1 01-03-2005 08:46 AM



Advertisments