Ivan Smirnov
Ivan Smirnov

Reputation: 4435

Undefined reference in g++ 7 when inheriting from std::vector

Here is a piece of code which fails to compile under g++-7.2 and clang-5.0 and compiles perfectly under g++-5 (-std=c++11).

#include <vector>

template<typename T>
class Array : private std::vector<T> {
public:
    typedef std::vector<T> Base;

    using Base::Base;
};

int main() {
    Array<int> a((int*)nullptr, (int*)nullptr);
}

The error arises at the linking stage: main.cpp:(.text+0x11): undefined reference to 'std::allocator<int>::allocator()'.

If I declare my array as Array<int> a, it compiles perfectly.

What's the problem here? As far as I understand, instantiation of template Array<int> enforces instantiation of std::vector<int>, which, in order, instantiates std::allocator<int>.

Demo with g++-5

Demo with g++-7

P.S. I'm aware of possible pitfalls in inheriting from STL containers. Here inheritance is private, and this answer claims that it is allowed. Anyway, the question is not about whether it is a good practice or not.

P.P.S. I would be glad if you suggest a better title.

Upvotes: 2

Views: 305

Answers (1)

pe3k
pe3k

Reputation: 794

There was a change on semantics of inherited constructors in GCC7. However, you can compile your code using -fno-new-inheriting-ctors switch.

More information is in GCC7 changelog: https://gcc.gnu.org/gcc-7/changes.html and P0136.

Upvotes: 1

Related Questions