Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C++ Builder cannot compile this?

I'm using this code:

#include <iostream>
#include <memory>
#include <vector>
using namespace std;

void out(int* p){
    cout << *p;
}

int main(){
    vector<unique_ptr<int> > vInt;

    for(int i = 0; i < 10; i++)
        vInt.push_back(unique_ptr<int>(new int(i)));

    out(vInt[0].get()); // 0
    return 0;
}

If I use some online compiler it compiles OK but both C++ Builder XE2 and XE6 report errors:

[bcc32 Error] vector(1179): E2247 'unique_ptr<int,default_delete<int> >::unique_ptr(const unique_ptr<int,default_delete<int> > &)' is not accessible
[bcc32 Error] vector(1203): E2247 'unique_ptr<int,default_delete<int> >::unique_ptr(const unique_ptr<int,default_delete<int> > &)' is not accessible
[bcc32 Error] xutility(1682): E2247 'operator unique_ptr<int,default_delete<int> >::=(const unique_ptr<int,default_delete<int> > &)' is not accessible
[bcc32 Error] xutility(1552): E2247 'operator unique_ptr<int,default_delete<int> >::=(const unique_ptr<int,default_delete<int> > &)' is not accessible
[bcc32 Error] xmemory(28): E2247 'unique_ptr<int,default_delete<int> >::unique_ptr(const unique_ptr<int,default_delete<int> > &)' is not accessible

I use default compiler/IDE settings do I don't know why this happens?

Can someone having C++ Builder XE3 or later confirm this issue?

like image 997
Tracer Avatar asked Nov 25 '25 16:11

Tracer


1 Answers

It's a compiler bug/failure to implement C++11 standards correctly. push_back has two overloads, one takes a const lvalue reference, another rvalue reference. Your code is perfectly fine since in vInt.push_back(unique_ptr<int>(new int(i))); the created unique_ptr is a temporary, which should be bound to rvalue reference and then moved into storage.

I don't have C++ builder so I don't know what workarounds will work. If it happens to implement emplace_back you can use that. Otherwise, you may instead resort to boost::container::vector or boost.ptr_container.

like image 100
Siyuan Ren Avatar answered Nov 28 '25 04:11

Siyuan Ren



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!