c++ - 将 std::string 转换为 ci_string

标签 c++ casting typedef case-sensitive char-traits

我用了this approach为字符串创建不区分大小写的 typedef。现在,我正在尝试将 std::string 转换为 ci_string。以下所有抛出编译器错误:

std::string s {"a"};
ci_string cis {s};
ci_string cis (s);
ci_string cis {(ci_string)s};
ci_string cis ((ci_string)s);
ci_string cis = s;

我花了一些时间试图找出如何重载 = 运算符,并且我尝试使用 static_castdynamic_cast 但没有成功。我该怎么做?

最佳答案

您的两种类型不同,因此您不能将构造函数与常规 std::string 一起使用。但是您的字符串仍然能够复制 C 字符串,所以这应该有效:

std::string s{"a"};
ci_string cis{ s.data() }; // or s.c_str(), they are the same

关于c++ - 将 std::string 转换为 ci_string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722743/

相关文章:

c++ - 区分 typedef

c++ - 派生类的方法需要向下转换其参数

c++ - 处理 std::wstring 和 std::string 之间的 UTF-8 编码字符串

Linq 输出作为接口(interface)?

casting - Perl 6 nativecast() 到具有 repr ('CPointer' ) 的对象时 GC 会破坏吗?

c - 对 C 中一个元素的数组执行结构的 typedef 可能产生的副作用

c++ - gluLookAt 的兼容性函数

c++ - 为什么 gcov 为 STL header 创建代码覆盖率数据?

java - 可以在 ServletContext 中存储 HashMap 吗?

c - 取消引用指向不完整类型的指针 - typedef struct