Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;11使用std::string::c_str()时的类型转换heisenbug_C++_C++11_Type Conversion_Xcode6 - Fatal编程技术网

C++ C++;11使用std::string::c_str()时的类型转换heisenbug

C++ C++;11使用std::string::c_str()时的类型转换heisenbug,c++,c++11,type-conversion,xcode6,C++,C++11,Type Conversion,Xcode6,这很奇怪 OSX 10.10 LLVM 6.0 XCode 6.1 test_assert("Wierd", String{"ABC"}, "ABC" ); // claims not equal String是我的自定义类(包装Python字符串原语),它应该通过此测试 下面是test_assert,添加了调试输出: template <typename B, typename V> static void test_assert( std::string description

这很奇怪

OSX 10.10 LLVM 6.0 XCode 6.1

test_assert("Wierd", String{"ABC"}, "ABC" ); // claims not equal
String是我的自定义类(包装Python字符串原语),它应该通过此测试

下面是test_assert,添加了调试输出:

template <typename B, typename V>
static void test_assert( std::string description, B benchmark, V value )
{
    std::ostringstream full_description;

    full_description << description
        << " : { " << "benchmark"      << ", " << "value"          << " }"
        << " = { " << typeid(B).name() << ", " << typeid(V).name() << " }"
        << " , { " << benchmark        << ", " << value            << " }";

    // N2Py6StringE, PKc  i.e. Py::String and const char* (Pointer to Konst Char)
    std::cout << typeid(B).name() << ", " << typeid(V).name() << std::endl;

    V b_as_v{static_cast<V>(benchmark)};

    // wtf? b_as_v: \352\277_\377 -- should be "ABC"
    std::cout << "b_as_v: " << b_as_v << std::endl; // Y

    if( b_as_v == value )
        std::cout << "    PASSED: " << full_description.str() << std::endl;
    else
        throw TestError( full_description.str() );
}
模板
静态无效测试断言(标准::字符串描述,B基准,V值)
{
std::ostringstream完整描述;

完整描述一个相当复杂的设置,但原因很简单:

explicit operator const char*() const
{
    std::string s{ as_std_string() };
    const char* c{ s.c_str() };

    // c before return: ABC
    std::cout << "c before return: " << c << std::endl; // X

    return c;
}
显式运算符const char*()const
{
std::string s{as_std_string()};
const char*c{s.c_str()};
//c返回前:ABC

std::cout一个相当复杂的设置,但原因很简单:

explicit operator const char*() const
{
    std::string s{ as_std_string() };
    const char* c{ s.c_str() };

    // c before return: ABC
    std::cout << "c before return: " << c << std::endl; // X

    return c;
}
显式运算符const char*()const
{
std::string s{as_std_string()};
const char*c{s.c_str()};
//c返回前:ABC

std::cout一个相当复杂的设置,但原因很简单:

explicit operator const char*() const
{
    std::string s{ as_std_string() };
    const char* c{ s.c_str() };

    // c before return: ABC
    std::cout << "c before return: " << c << std::endl; // X

    return c;
}
显式运算符const char*()const
{
std::string s{as_std_string()};
const char*c{s.c_str()};
//c返回前:ABC

std::cout一个相当复杂的设置,但原因很简单:

explicit operator const char*() const
{
    std::string s{ as_std_string() };
    const char* c{ s.c_str() };

    // c before return: ABC
    std::cout << "c before return: " << c << std::endl; // X

    return c;
}
显式运算符const char*()const
{
std::string s{as_std_string()};
const char*c{s.c_str()};
//c返回前:ABC

std::cout Thank T.C.,这是否意味着我可以安全返回const char*的唯一方法是对其进行malloc运算,并要求使用者随后释放它?@Pi或在
string
类中存储
std::string
,或者只返回
std::string
并让使用者调用
.C_str()
在上面。谢谢T.C.,这是否意味着我可以安全返回const char*的唯一方法是对其进行malloc运算,并要求使用者随后释放它?@Pi或在
string
类中存储
std::string
。或者只返回
std::string
并让使用者调用
.C_str()
在上面。谢谢T.C.,这是否意味着我可以安全返回const char*的唯一方法是对其进行malloc运算,并要求使用者随后释放它?@Pi或在
string
类中存储
std::string
。或者只返回
std::string
并让使用者调用
.C_str()
在上面。谢谢T.C.,这是否意味着我可以安全返回const char*的唯一方法是对其进行malloc运算,并要求使用者随后释放它?@Pi或在
string
类中存储
std::string
。或者只返回
std::string
并让使用者调用
.C_str()
在上面。