为什么删除了我的基类构造函数之一? (C ++ 11)

时间:2022-09-02 10:05:47

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

我写了一个简单的C ++ 11程序,在其中我创建了一个派生自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:

当然,my_vec除了std :: vector之外没有做任何事情,但那是因为我在缩小错误时删除了所有额外的功能。这个程序在Linux上的g ++ 4.8.1编译得很好,但是当在OS X上使用clang 500.2.79时,它会给我以下错误:

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.

为什么clang坚持认为std :: vector的范围构造函数已被删除?所有其他构造函数似乎都被遗传得很好。

2 个解决方案

#1


1  

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.

您的OS X编译器基于llvm clang-3.3(在google上检查),来自此站点http://clang.llvm.org/cxx_status.html看起来继承构造函数应该可以从版本3.3获得,但它看起来像它的在这个版本中实现是错误的。

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

我用clang 3.5检查了ubuntu并且你的示例代码编译得很好。

#2


3  

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

#1


1  

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.

您的OS X编译器基于llvm clang-3.3(在google上检查),来自此站点http://clang.llvm.org/cxx_status.html看起来继承构造函数应该可以从版本3.3获得,但它看起来像它的在这个版本中实现是错误的。

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

我用clang 3.5检查了ubuntu并且你的示例代码编译得很好。

#2


3  

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