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

Discussion in 'C++' started by Atlas, Oct 20, 2005.

1. ### AtlasGuest

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.

Atlas, Oct 20, 2005

2. ### benGuest

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

ben, Oct 20, 2005

3. ### KristoGuest

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

Kristo, Oct 20, 2005
4. ### AtlasGuest

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;
}

Atlas, Oct 21, 2005