templatized class inside templatized class

This is a discussion on templatized class inside templatized class within the C++ Programming forums, part of the General Programming Boards category; oh that's an easy one; you should know that! the native types: int, char, unsigned int, float, double, long, etc....

  1. #61
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    oh that's an easy one; you should know that! the native types:

    int, char, unsigned int, float, double, long, etc.

  2. #62
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by -EquinoX- View Post
    so I came to a conclusion that there is no way to make sure that type has all the operators:

    =, ==, !=, +, +=, -, -=
    Requiring the type to have all of those operators is probably too strict a requirement, since !=, +, and - can be represented in terms of the other ones.

    If you see a != b, you can replace with !(a == b)

    If you see a + b, you can replace with a temporary which is copy-constructed or assigned from a, then invoke += b on it.

    Similar for a - b.

    I would only require =, ==, +=, -=
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #63
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    One way to avoid the problem of operators not implemented by the class is to simply use a template function. That way, as long as the user doesn't try to invoke the operator, no error occurs. For example:

    Code:
    template < typename T >
    struct test
    {
        template < typename U >
        test& operator *= ( U const& rhs )
        {
            value *= rhs;
            return *this;
        }
    
        T value;
    };
    
    int main( void )
    {
        test< string > ts; // fine
        ts *= string( ); // error
    }
    Quote Originally Posted by brewbuck View Post
    Requiring the type to have all of those operators is probably too strict a requirement, since !=, +, and - can be represented in terms of the other ones.

    If you see a != b, you can replace with !(a == b)

    If you see a + b, you can replace with a temporary which is copy-constructed or assigned from a, then invoke += b on it.

    Similar for a - b.

    I would only require =, ==, +=, -=
    Yep, excellent suggestion.

  4. #64
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay, I have another question now regarding operators between two vectors of different size:
    What does this sentence mean:

    If you check for mismatched sizes, but it is impossible for the sizes to be mismatched based on strong typing (i.e., the compiler will fail)

    Can you give me an example of when this could happen and how to prevent this in my vector template class.

  5. #65
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    I'm not sure what that actually means, honestly. Can you provide the complete requirements (or at least a link to it)?

  6. #66
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    The full requirements:

    When operations between vectors of unequal size are performed, exceptions should be thrown, or the compile should fail. If you check for mismatched sizes, but it is impossible for the sizes to
    be mismatched based on strong typing (i.e., the compiler will fail),

  7. #67
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    No, I mean the full requirements for the entire assignment. That might aid in infering the meaning from the context of everything else. From what I can tell, checking for mismatched sizes doesn't even require an operation on the element type, so there should never be a complication error in the first place. I was hoping that maybe the rest of requirements might shed some light on the statement in question.

  8. #68
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I can't copy the whole assignment here.

  9. #69
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Sebastiani
    One way to avoid the problem of operators not implemented by the class is to simply use a template function. That way, as long as the user doesn't try to invoke the operator, no error occurs.
    That is already the case with the member functions of the class template.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #70
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    it's important to remember that classes instantiated from the same template, but with different template parameters are totally separate types. they are not related to each other. a foo<int,2> is a different type than a foo<int,3>.

    thus any template class method that accepts expects its own type as a function parameter cannot accept a class instantiated from the same template with different parameters.

    do you follow?

  11. #71
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    the reason to have the type take an int is so that I can do the following:

    Code:
    vector<class, 2> temp;
    vector<class, 2> temp 2;
    
    temp2[0] = 1;
    temp2[1] = 2;
    
    vector<int, 2> result = temp + temp2;
    it is said to initialize temp to 0, but how can this be possible with a type taking an int?

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an object of a class inside another class - possible?
    By Programmer_P in forum C++ Programming
    Replies: 34
    Last Post: 08-15-2009, 11:21 PM
  2. Replies: 2
    Last Post: 11-28-2008, 04:15 PM
  3. Unresolved external on vector inside class constructor
    By Mario F. in forum C++ Programming
    Replies: 13
    Last Post: 06-20-2006, 12:44 PM
  4. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  5. how do i define a const inside a class under private
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2001, 05:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21