小编Kis*_*imo的帖子

C++ string.c_str()

如果使用g++clang++,我得到++my string==my string##my string--.虽然是MSVC和英特尔编译器++==my string##my string--.

为什么?

#include <string>
#include <iostream>

using namespace std;

string test()
{
    string s0 = "my string";
    return s0;
}

int main()
{
    string s = test();
    const char* s1 = test().c_str();
    const char* s2 = s.c_str();
    cout << "++" << s1 << "==" << s2 << "##" << test().c_str() << "--" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是一个未定义的行为吗?

c++ undefined-behavior

0
推荐指数
1
解决办法
272
查看次数

标签 统计

c++ ×1

undefined-behavior ×1