简体   繁体   中英

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.

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() );

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM