2

I am trying to inherit the std::vector class template into my membvec class template as public. And I want to use it as e.g. say membvec<float> mymemb(10) with the intention of creating my membvec variable mymemb containing 10 elements.

But I can't figure out how to write the templatised declaration of the public inheritance. What I am doing is the following, but all in vain.

template <typename T, template <typename T> class std::vector = std::vector<T>>
//error above: expected '>' before '::' token
class membvec: public std::vector<T>
{
    const membvec<T> operator-() const; // sorry the previous version was a typo 
    //error above: wrong number of template arguments (1, should be 2)
    ...
};
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
  • 4
    You might want to read [this](http://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector). Do you really need to inherit from `std::vector`? – TartanLlama Mar 16 '16 at 13:57
  • 1
    Is there a reason you need to inherit from vector? Normally just adding global free functions to extend its functionality works. – NathanOliver Mar 16 '16 at 13:58
  • Yes, I want to feed it to another function which requires addition, subtraction, multiplication and division operators to be defined in addition to generic `std::vector` operations. –  Mar 16 '16 at 14:00
  • 5
    You're over-complicating things. The only template parameter you need is `T`. But I don't think you need public inheritance at all. – juanchopanza Mar 16 '16 at 14:02
  • @juanchopanza, allegedly, OP wants other containers as well? The syntax would be `template class container = std::vector> class membvec : public container` – SergeyA Mar 16 '16 at 14:04
  • @SergeyA "I am trying to inherit the std::vector..." So I doubt that. – juanchopanza Mar 16 '16 at 14:05
  • Wait wait! Could I get a solution for my particular case, and not in general? Well then things will be simpler for me to understand the syntax. In fact, I really don't know all the nitty-gritty of the `std::vector` template parameters. These `container` things are confusing me. Any pointer would be of great help. –  Mar 16 '16 at 14:05
  • 3
    smql, in this case, you really don't need anything else but a single T, as @juanchopanza says. I thought, you want to be able to use other containers as well, but if it is set to be vector, all you need is `template class membvec : public std::vector` – SergeyA Mar 16 '16 at 14:07
  • Well I started something with `template `. I will check it. Thanks. Seems like it will work. –  Mar 16 '16 at 14:11
  • This part is complete nonsense: `template class std::vector = std::vector` ... a template parameter named "std::vector" which is a std::vector ... wat? You don't need `std::vector` in the template parameter list at all, the only template parameter you need is `T`. If you want to derive from `std::vector` then just do that, don't make it part of the template parameter list. – Jonathan Wakely Mar 16 '16 at 16:00

2 Answers2

2

I think you're looking for something like the below, but seriously don't do it. If you ever pass your class as its parent std::vector, there is no virtual interface to allow your class to provide any benefit whatsoever. And if you don't need to substitute for a std::vector then there's no need to inherit from it. Prefer free function algorithms or containing the std::vector as a member in your class instead.

#include <vector>

template <typename T>
class membvec: public std::vector<T>
{
    // Don't need <T> in class scope, must return by value.
    membvec operator+() const;
};

int main()
{
    membvec<int> foo;
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • My typo for the `return by reference`. I just typed something before I ended up with this `declaration` problem. –  Mar 16 '16 at 16:33
1

Perhaps you want something like this:

#include <vector>                                                   

template <typename T, template <typename T, class Allocator> class Vec = std::vector>
class membvec: public Vec<T, std::allocator<T>>                                                                                             
{
public:
    // This is the signature in your question, but it's questionable.
    const membvec<T, Vec> &operator+(int x) const
    {
        // You obviously want to change this.
        return *this;
    }
};

You can then use it regularly:

int main()
{
    membvec<char> foo;
    foo + 3;
}
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185