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

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

  1. Atlas

    Atlas Guest

    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
    #1
    1. Advertising

  2. Atlas

    ben Guest

    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
    #2
    1. Advertising

  3. Atlas

    Kristo Guest

    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
    #3
  4. Atlas

    Atlas Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Agoston Bejo
    Replies:
    1
    Views:
    387
    Jonathan Turkanis
    Jan 3, 2005
  2. Replies:
    6
    Views:
    342
    Victor Bazarov
    May 10, 2005
  3. Anthony Liu
    Replies:
    0
    Views:
    259
    Anthony Liu
    Mar 1, 2005
  4. Replies:
    3
    Views:
    364
    Rolf Magnus
    Apr 5, 2006
  5. Replies:
    7
    Views:
    577
Loading...

Share This Page