CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2003
    Location
    Heilbronn
    Posts
    48

    Unhappy more templates at once?

    I'm really unexperienced, so maybe it's nonsense. But I would like to know whether is there a possibility to use many different templates in one method or operator like:

    template < class T, int x, int y> class CMatrix
    {
    // blah blah blah...
    }

    template < class T, int x, int z>
    template < class T, int y, int z>

    CMatrix < T, x, z> CMatrix< T, x, y> :: operator * ( CMatrix < T, y, z> m)
    {
    CMatrix < T, x, z> r;
    // blah blah blah
    return r;
    }

    I know, syntax is not correct, template keyword shouldn't be used like above, but if You know how to handle this problem I appreciate any advice.
    Last edited by carnaz; April 2nd, 2003 at 03:15 AM.

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Don't make the matrix sizes template parameters.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I don't even understand the question. You ask about using
    multiple templates, but your example has only one template.
    Do you mean multiple template parameters? If so, yeah, you can
    use more than one template parameter. Look at your compiler's
    implementation of the C++ Standard Library to see some
    examples; even std::vector uses multiple template parameters.

    --Paul

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    The following compiles:
    Code:
    template <typename T, int x, int y>
    class Matrix
    {
    public:
        template <int z>
        Matrix<T, x, z> operator*(const Matrix<T, y, z>& m);
    };
    
    template <typename T, int x, int y, int z>
    Matrix<T, x, z> Matrix<T, x, y>::operator*(const Matrix<T, y, z>& m)
    {
    }
    However, I haven't tested it to see if the compiler will do the correct resolution operations on it in use.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I just tried the following (with the above definitions) in VC++6.0
    Code:
        Matrix<int, 2, 3> mat23;
        Matrix<int, 3, 4> mat34;
        Matrix<int, 2, 4> mat24;
        
        mat24 = mat23*mat34;
    and got an internal compiler error. So, no surprises there. VC++6 is notoriously bad at handling templates properly.

    I compiled it with Comeau and it said
    "ComeauTest.c", line 11: error: declaration is incompatible with function template
    "Matrix<T, x, z> Matrix<T, x, y>:perator*(const Matrix<T, y, z> &)"
    (declared at line 7)
    Matrix<T, x, z> Matrix<T, x, y>:perator*(const Matrix<T, y, z>& m)
    So I guess that isn't correct, either. I'll have a look and see if Bjarne's got the answer.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    The answer is:
    Code:
    template <typename T, int x, int y>
    class Matrix
    {
    public:
        template <int z>
        Matrix<T, x, z> operator*(const Matrix<T, y, z>& m);
    };
    
    template <typename T, int x, int y>
    template <int z>
    Matrix<T, x, z> Matrix<T, x, y>::operator*(const Matrix<T, y, z>& m)
    {
    }
    BTW: VC++6 can't cope with this, but it is correct according to Comeau.
    Last edited by Graham; April 2nd, 2003 at 08:42 AM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Sep 2002
    Posts
    1,747
    Originally posted by Gabriel Fleseriu
    Don't make the matrix sizes template parameters.
    Using non-type template parameters for matrix size can be useful for optimizations. In particular, you can force inline easier, make decisions on algorithm choice based on size without leaving costly conditionals in the compiled code, and even allow the compiler to deal with some of the information which is known at translationtime. Unfortunately, it does eliminate the possibility of runtime dynamic matrices, but many of the calculations and simulations I have been involved with actually did not require that type of flexibility.

    carnaz, Graham's excellent answer should get you started. Note that it is a parametrized method of a parametrized class, and that the class template parameters come before the additional parameter in the specification. You only need to declare the further abstraction needed to complete the signature in the method's template parameter, and not any of the full <> collections as in your posted attempt, as the type matching will be done throughout.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I feel that I ought to point out that a work-around for VC++6 is to put the function body inside the class definition, rather than outside:
    Code:
    template <typename T, int x, int y>
    class Matrix
    {
    public:
        template <int z>
        Matrix<T, x, z> operator*(const Matrix<T, y, z>& m)
        {
            // .....
        }
    };
    Makes the class declaration harder to read, but it should compile (haven't tested it, though).
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured