1

I am creating a template class and inheriting std::vector. Along with all the constructors of std::vector, I want to overload the constructor of the derived class with a custom class. However when I do this, I get the compilation error for the default constructor call.

Code below:

template<typename T, unsigned int N>
class _std_vector : public std::vector<T>
{
    public:
        using std::vector<T>::vector;
        _std_vector(const MyCustomClass& that)
        {
            //Do Something
        }
}

And I call as follows:

MyCustomClass my;
_std_vector<int, 8> sv{ my};

The above works fine the moment I overload the constructor. However the default non parametrized constructor gives compilation error.

Error C2512 '_std_vector< int,8>': no appropriate default constructor available

_std_vector <int, 8> svv; //This works fine until I add _std_vector(const MyCustomClass& that)

However please note, all the other parametrized constructor of std::vector seem to compile fine.

Am I missing something here?

Justin
  • 24,288
  • 12
  • 92
  • 142
Ajay Nair
  • 1,827
  • 3
  • 20
  • 33
  • 5
    Just don't inherit from `stl::vector` or any other standard container. There is never a good reason to do that. – SergeyA Jul 19 '18 at 17:50
  • 1
    [Don't inherit from standard containers](https://stackoverflow.com/questions/6806173/subclass-inherit-standard-containers). Just create a free function `std::vector toVector(const MyCustomClass& that)` and avoid much trouble – alter_igel Jul 19 '18 at 17:52
  • However, this question would be valid if it would not be `std::vector` you are inheriting from. If you are interested in that, you can ask another question or edit this one. – SergeyA Jul 19 '18 at 17:53
  • 1
    I'm voting to close this question as off-topic because OP should not inherit from STL's vector, and there is nothing to debate. – SergeyA Jul 19 '18 at 17:53
  • 7
    @SergeyA That's a poor reason. It doesn't violate any site guide lines and the practice isn't strictly speaking disallowed. And questions *shouldn't* start a debate, those that do should be closed for being opinion based. – François Andrieux Jul 19 '18 at 17:54
  • @FrançoisAndrieux probably the better closure would be a one of a thousands duplicates which explain why one shouldn't do this. Since I already cast my vote, you can cast a duplicate one. – SergeyA Jul 19 '18 at 17:56
  • 7
    @SergeyA I again disagree. This question isn't asking whether or not inheriting from `std::vector` is a good idea. The question is asking why the default constructor stops working in this case. It doesn't have anything to do with inheriting standard containers and it's just unfortunate that it's the example that was used to illustrate the question. Links to questions explaining why this inheritance is a bad idea have already been posted and that should be sufficient. – François Andrieux Jul 19 '18 at 17:58
  • 1
    Your sample code doesn't fail for the appropriate reason: https://godbolt.org/g/ofDBbV . – Justin Jul 19 '18 at 17:58
  • Shouldn't `using std::vector;` be `using std::vector::vector;` – NathanOliver Jul 19 '18 at 18:00
  • If I assume you made a typo and meant `using std::vector::vector;`, I can't reproduce the compilation error (it compiles fine for me): https://godbolt.org/g/JebZwg – Justin Jul 19 '18 at 18:00
  • 1
    @Justin A More complete example: https://godbolt.org/g/hXhGMt (still compiles) – NathanOliver Jul 19 '18 at 18:03
  • @AjayNair [Your sample compiles here](https://godbolt.org/g/hXhGMt). Can you post a reproducible [mcve] that fails to compile? – NathanOliver Jul 19 '18 at 18:06
  • 1
    @NathanOliver Ah. The code was in multiple places, so I missed that. Yes, this doesn't compile on msvc: https://godbolt.org/g/D8Ns3n – Justin Jul 19 '18 at 18:06
  • Intresting. Clang and GCC are fine. ICC and MSVS both fail. Must be something to do with how they implement `vector`. – NathanOliver Jul 19 '18 at 18:09
  • Your class name is illegal, see details here https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – Slava Jul 19 '18 at 18:13
  • 1
    FWIW: here is a "better" MCVE: https://godbolt.org/g/EzqHJN – NathanOliver Jul 19 '18 at 18:35

0 Answers0