相关疑难解决方法(0)

const unsigned char*to std :: string

sqlite3_column_text返回一个const unsigned char*,如何将其转换为std :: string?我试过std :: string(),但是我收到了一个错误.

码:

temp_doc.uuid = std::string(sqlite3_column_text(this->stmts.read_documents, 0));
Run Code Online (Sandbox Code Playgroud)

错误:

1>.\storage_manager.cpp(109) : error C2440: '<function-style-cast>' : cannot convert from 'const unsigned char *' to 'std::string'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
Run Code Online (Sandbox Code Playgroud)

c++ std

40
推荐指数
3
解决办法
6万
查看次数

为什么VC++字符串不被引用计数?

STL标准不要求从std :: string中重新计算.但事实上,大多数C++实现都提供了refcounted,copy-on-write字符串,允许您将值作为基本类型传递.此外,这些实现(至少g ++)使用原子操作使这些字符串无锁和线程安全.

简单的测试显示了写时复制语义:

#include <iostream>
#include <string>

using namespace std;

void foo(string s)
{
    cout<<(void*)s.c_str()<<endl;
    string ss=s;
    cout<<(void*)ss.c_str()<<endl;
    char p=ss[0];
    cout<<(void*)ss.c_str()<<endl;
}

int main()
{
    string s="coocko";
    cout<<(void*)s.c_str()<<endl;
    foo(s);
    cout<<(void*)s.c_str()<<endl;
}
Run Code Online (Sandbox Code Playgroud)

在使用非常数成员后,只有两个地址完全打印.

我使用HP,GCC和Intel编译器测试了这段代码,得到了类似的结果 - 字符串可以作为写时复制容器.

另一方面,VC++ 2005清楚地表明每个字符串都是完全复制的.

为什么?

我知道VC++ 6.0中存在一个错误,它具有非线程安全的引用计数实现,导致随机程序崩溃.这是什么原因?他们只是害怕再使用ref-count,即使这是常见的做法?他们宁愿不使用重新计算来解决问题吗?

谢谢

c++ string reference-counting copy-on-write visual-c++

13
推荐指数
4
解决办法
5202
查看次数