Thread: templatized class inside templatized class

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

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

  2. #62
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,708
    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,708
    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,708
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #70
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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?

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, 05: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, 06:01 PM