gpt4 book ai didi

c++ - 为什么 g++ 与 -O3 段错误与以下 LLVM 库代码

转载 作者:行者123 更新时间:2023-11-27 22:48:43 24 4
gpt4 key购买 nike

所以我想使用 llvm::Twine 字符串 block 类。

我有以下示例:

#include <llvm/ADT/Twine.h>
#include <iostream>

int main()
{
llvm::Twine twine1 = llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd";
llvm::Twine twine2 = llvm::Twine(twine1) + "dddd" + "eeee";
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;

return 0;
}

它运行在 clang++-O3g++-O0 但与 g++ 的段错误-O3。我从 3.4-3.9 尝试了这段代码部分不同版本的 clang 库,并尝试使用 g++ 4.8.4g++ 4.8.5mingw-5.3.0.

您需要 llvm 库并将代码与 -lLLVMSupport -lLLVMCorellvm-config --ldflags 中的其他链接

最佳答案

来自 Twine documentation :

A Twine is not intended for use directly and should not be stored, its implementation relies on the ability to store pointers to temporary stack objects which may be deallocated at the end of a statement. Twines should only be used accepted as const references in arguments, when an API wishes to accept possibly-concatenated strings.

换句话说,Twine 对象不拥有它的部分,所以它们在语句结束时被销毁。

正确的用法是:

#include <llvm/ADT/Twine.h>
#include <iostream>

void bar(const llvm::Twine& twine1, const llvm::Twine2& twine2){
std::cout << twine1.str() << std::endl;
std::cout << twine2.str() << std::endl;
}

void foo(const llvm::Twine& twine1){
bar(twine1, twine1 + "dddd" + "eeee");
}

int main()
{
foo(llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd");

return 0;
}

关于c++ - 为什么 g++ 与 -O3 段错误与以下 LLVM 库代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com