C++Builder's 32-bit compiler does not support C++11 yet. Its 64-bit compiler does, as it is based on Clang 3.1. If you can't use auto_ptr either, then there is something wrong in your install as auto_ptr works in both compilers.
Why C++ Builder cannot compile this?
-
14-07-2023 - |
Question
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?
Solution 2
OTHER TIPS
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
.
I found the answer thanks to @GregorBrandt. C++ 11 is still not supported under 32 bit applications. Only for 64 bit.
https://forums.embarcadero.com/message.jspa?messageID=596431