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

A

Atlas

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.
 
B

ben

Atlas said:
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
 
K

Kristo

Atlas said:
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
 
A

Atlas

Kristo said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top