Ind*_*ant 0 c++ llvm clang c++11
所以我想使用llvm::Twine
字符串块类.
我有以下样本:
#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;
}
Run Code Online (Sandbox Code Playgroud)
它运行与clang++
使用-O3
,并g++
与-O0
但段错误g++
使用-O3
.我尝试了这个代码从3.4-3.9部分不同版本的clang库并试过g++ 4.8.4
,g++ 4.8.5
和mingw-5.3.0
.
您需要llvm库并将代码与-lLLVMSupport -lLLVMCore
其他代码链接起来llvm-config --ldflags
小智 5
来自Twine文档:
Twine不是直接使用的,不应该存储,它的实现依赖于存储指向临时堆栈对象的指针的能力,这些指针可能在语句结束时被释放.当API希望接受可能连接的字符串时,Twines只能在参数中被接受为const引用.
换句话说,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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
104 次 |
最近记录: |