Thread: templatized class inside templatized class

  1. #31
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    you mentioned above about constraints, where in the code that you suggested is the constraint that would enforce it to take an int as an argument?

    the += is just an operator overloading to add two vectors

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    you mentioned above about constraints, where in the code that you suggested is the constraint that would enforce it to take an int as an argument?
    The partial example that I gave does not make use of that. What it does make use of is a copy assignment operator, which I assume is among "the full set of operators provided below". If it is not... then your best option is to use a std::vector instead of an array of N elements (and if you are not allowed to use std::vector, then you should use placement new, and everything just becomes more complicated).

    Quote Originally Posted by -EquinoX-
    the += is just an operator overloading to add two vectors
    Sure, but what does "add two vectors" mean? You have to be clear on that, since it could mean the pairwise addition of the elements of the two vectors, or it could mean the concatenation of one vector to the other, or something else.
    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

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I guess it would add each element that is the vector with each element on the other vector, if the size doesn't match it would throw an exception..

  4. #34
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    here's the continuation of the instruction, if you wish to know the full set of operators provided below are:

    Your template class should provide at least the following class or global operator methods:
    1 =, ==, !=, +, +=, -, -=, [ ]. Both binary, and unary, operator- should be provided.
    2 In operations with scalar type values, the operators *, /, *=, /= can be used.

    which raises up a new question, how can the element type also have the following operators...
    Last edited by -EquinoX-; 11-17-2009 at 01:10 AM.

  5. #35
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Don't you want something like

    Code:
    vector& operator*=(const vector& other)
    {
         if (this->size != other.size)
             throw std::length_error("No matching size");
         for(int i=0; i < this->size; ++i)
             this->elements[i] *= other.elements[i];
        return *this;
    }
    or
    Code:
    vector& operator*=(const vector& other)
    {
         for(int i=0; i < this->size && i < other.size; ++i)
             this->elements[i] *= other.elements[i];
         return *this;
    }
    Last edited by C_ntua; 11-17-2009 at 01:52 AM.

  6. #36
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    but it only says for operation with scalar values... so how can I know if it's scalar or not, is it directly saying that if I am storing a vector of char or string then I can't use this

  7. #37
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    anyone care to give me some insights?

  8. #38
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    template<typename T,size_t N>foo
    {
      private:
        T  arr[N];
      public:
        foo& operator*=(T t)
        {
           for(size_t i=0;i<N;++i)
           {
              arr[i]*=t;
           }
           return *this;
        }
    };

  9. #39
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    what if what's stored in i can't be multiplied by t? what if T is a char or a string?

    also after giving it some thought, I think type is a class, as the vector is storing an object of type... therefore type is another class... I just don't know how can I make so that type can implement the same operator that vector has, such as *=, etc and I don't know the purpose of type having an int as an argument... if type is a string, then the string takes an int as an argument... but what is that int for in a string? any ideas?
    Last edited by -EquinoX-; 11-17-2009 at 10:18 PM.

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by -EquinoX-
    so how can I know if it's scalar or not, is it directly saying that if I am storing a vector of char or string then I can't use this
    A char is a scalar.

    Quote Originally Posted by -EquinoX-
    what if what's stored in i can't be multiplied by t? what if T is a char or a string?
    Then that is the user's problem (though in the case of char or any integer/floating point type, overflow is also a problem, but at run time). In some later version of C++ there may be the concept of concepts, where it can be clearly enforced that a template can only be instantiated with a type that supports certain operations, but for now (and perhaps even in C++0x) such a feature does not exist, though Boost.ConceptCheck makes an attempt at it.

    Quote Originally Posted by -EquinoX-
    also after giving it some thought, I think type is a class, as the vector is storing an object of type... therefore type is another class... I just don't know how can I make so that type can implement the same operator that vector has, such as *=, etc and I don't know the purpose of type having an int as an argument... if type is a string, then the string takes an int as an argument... but what is that int for in a string? any ideas?
    It is the other way round: you tell your users: my template works with types that support these operations with these semantics. If you instantiate it with a type that does not support these operations or with the wrong semantics, good luck.
    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

  11. #41
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    look at std::valarray.

  12. #42
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I think the only way I can restraint it is for the template class vector to take an object type... type can be anything but then I can guarantee that type has all the overload operators

  13. #43
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    no you can't. you're just kicking the can down the road.

    the point of making that restriction is that your vector class isn't supposed to be used with any type!

    if you make a Type<std::string> you're going to get a compilation error in Type just the same as you with vector<std::string> if you ever use *= operator of the class.

    you're still looking at the phrasing of the assignment backwards:

    2 In operations with scalar type values, the operators *, /, *=, /= can be used.
    you're looking at this as a requirement of your class to provide a way to support those operators no matter what stupid thing gets specified as a template parameter type.

    what that really means is that is the operators that can be used with your vector - it's a requirement of the parameter types, not your vector! you must provide them and use the parameter type's operator, but you are not supposed to try to support an operator that doesn't exist.

    besides, you'll see that eventually, if you ever intend to use those operators, the underlying paramter type must have them implemented anyway, so your strategy just adds a layer of complication with no gain over the simpler implementation i am suggesting to you.
    Last edited by m37h0d; 11-18-2009 at 09:29 AM.

  14. #44
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    #include <valarray>
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      std::valarray<std::string>thisMakesNoSense;
      thisMakesNoSense*="asdf";
    	return 0;
    }

  15. #45
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well it's a requirement for both the vector and the type parameter... I am only confused on how can I make so that whatever type is the parameters then it would support those operators

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