【问题标题】:C++ Builder bccarm error when calling std::vector::push_back with TObject descendant使用 TObject 后代调用 std::vector::push_back 时 C++ Builder bccarm 错误
【发布时间】:2016-09-05 05:09:59
【问题描述】:

我有一些简单的 C++ 代码,它们不会被 C++ Builder 10.1 Berlin 的基于 Clang 的 C++11 编译器 bccaarm 编译。

这是代码:

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

这是错误:

[bccaarm 错误] stl_iterator.h(963): 对类型的右值引用 'value_type' (aka 'System: classes::TComponent * __strong') 不能 绑定到“__borland_class * isTObj __strong”类型的左值(又名 'System::Classes::TComponent * __strong')

编译器在文件 stl_iterator.h 的第 963 行停止:

其他 C++ 编译器 bcc32 和 bcc32c(也基于 Clang)对此代码没有问题。

Comp不是来自TComponent类型或TObject的另一个后代时,代码编译没有任何问题。

我不知道这段代码有什么问题,也不知道为什么 R 和 L 值有问题...

有人知道在这里做什么吗?

【问题讨论】:

  • 英文翻译是这样的:[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') 移动编译器,如 bccaarm,实现 Object ARC(这就是 __strong 发挥作用的地方),但桌面编译器没有,这就是代码在 bcc32 和 bcc32c 中编译的原因。
  • 我希望这段代码在 ARC 下编译得很好,虽然我觉得 std::vector::iterator::operator* 使用 std::move() 很奇怪。运算符应该返回对向量中现有项目的引用,不应该涉及移动。也许是一个 STL 错误?
  • @RemyLebeau:添加 __unsafe 有帮助(请参阅答案),但仍不清楚 STL 中是否存在错误...

标签: android c++ c++builder firemonkey c++builder-10.1-berlin


【解决方案1】:

要编译上述代码,向量类型必须定义为不安全指针。

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

我为我遇到的另一个问题打开了一个支持案例。 embarcadero 支持给了我以下信息,我将这些信息应用于这个问题,它似乎有效:

__unsafe 告诉编译器将处理对象生命周期,并且不会为对象生成 ARC 代码

有关此主题的更多信息:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多