-->

C++ Builder bccarm error when calling std::vector:

2020-03-30 07:21发布

问题:

I have some simple C++ code which won't be compiled by the Clang based C++11 compiler bccaarm of C++ Builder 10.1 Berlin.

This is the code:

TComponent* Comp = new TComponent(this);
std::vector<TComponent*> Comps;
Comps.push_back(Comp);

And this is the error:

[bccaarm error] stl_iterator.h(963): rvalue reference to type 'value_type' (aka 'System: classes::TComponent * __strong') can not be bound to lvalue of type '__borland_class * isTObj __strong' (aka 'System::Classes::TComponent * __strong')

The compiler stops at line 963 in the file stl_iterator.h:

The other C++ compilers bcc32 and bcc32c(also Clang based) have no problems with this code.

When Compis not from type TComponent or another descendant from TObject the code compiles without any problem.

I have no idea what is wrong with this code and why there is a problem with R and L values...

Does anybody know what to do here?

回答1:

To get the above code compiled the vector type has to be defined as an unsafe pointer.

TComponent* Comp = new TComponent(this);
std::vector<__unsafe TComponent*> Comps;
Comps.push_back(Comp);

I openened a support case for an other problem I had. The embarcadero support gave me the following information which I applied to this problem and it seems to work:

__unsafe tells the compiler that object lifetimes will be handled and no ARC code is generated for the objects

More about this topic:

http://docwiki.embarcadero.com/RADStudio/Berlin/en/Automatic_Reference_Counting_in_C%2B%2B#weak_and_unsafe_pointers