Aaron Leung
Aaron Leung

Reputation: 43

Why is one of my base class constructors deleted? (C++11)

I wrote a simple C++11 program in which I create a class that's derived from std::vector:

#include <vector>

using namespace std;

template <typename T>
class my_vec : public vector<T> {
public:
  using vector<T>::vector;
};

int main() {
  my_vec<int> v0;
  my_vec<int> v1 { 1, 2, 3, 4, 5 };
  my_vec<int> v2 ( 42 );
  my_vec<int> v3 ( v1 );
  my_vec<int> v4 ( v1.begin()+1, v1.end()-1 );
  return 0;
}

Granted, my_vec doesn't do anything over and above std::vector, but that's because I removed all my extra functionality in narrowing down my error. This program compiles fine on g++ 4.8.1 on Linux, but when using clang 500.2.79 on OS X, it gives me the following error:

myvec.cpp:16:15: error: call to deleted constructor of 'my_vec<int>'
  my_vec<int> v4 ( v1.begin()+1, v1.end()-1 );
              ^    ~~~~~~~~~~~~~~~~~~~~~~~~
myvec.cpp:8:20: note: function has been explicitly marked deleted here
  using vector<T>::vector;
                   ^
1 error generated.

Why does clang insist that std::vector's range constructor has been deleted? All the other constructors seem to have been inherited just fine.

Upvotes: 2

Views: 156

Answers (2)

marcinj
marcinj

Reputation: 50016

Your OS X compiler is based on llvm clang-3.3 (checked on google), from this site http://clang.llvm.org/cxx_status.html it looks like inheriting constructors should be available from version 3.3, but it looks like its implementation is buggy in this version.

I checked on ubuntu with clang 3.5 and your sample code compiles fine.

Upvotes: 1

Martin J.
Martin J.

Reputation: 5118

It probably has something to do with the fact that the range constructor is not a regular method, but a template method.

template< class InputIt >
vector( InputIt first, InputIt last, 
    const Allocator& alloc = Allocator() );

Upvotes: 3

Related Questions