C++用户定义的没有类的转换运算符?
在 C++ 中是否可以定义不是类成员的转换运算符?我知道如何对常规运算符(例如 +)执行此操作,但不知道如何对转换运算符执行此操作。
这是我的用例:我使用一个 C 库,它向我提供一个 PA_Unichar *
,其中该库将 PA_Unichar 定义为 16 位 int。它实际上是一个以UTF-16编码的字符串。我想将其转换为以 UTF-8 编码的 std::string
。我已准备好所有转换代码并可以使用,我只缺少允许我编写的语法糖:(
PA_Unichar *libOutput = theLibraryFunction();
std::string myString = libOutput;
通常在没有临时变量的一行中)。
另外值得注意的是:
我知道
std::string
没有定义从char*
的隐式转换,我知道为什么。同样的原因可能适用于此,但这不是重点。我确实有一个
ustring
,它是std::string
的子类,它定义了PA_Unichar*
的正确转换运算符。它可以工作,但这意味着使用ustring
变量而不是std::string
并且 then 需要转换为std::string 当我将这些字符串与其他库一起使用时。所以这并没有多大帮助。
使用赋值运算符不起作用,因为它们必须是类成员。
使用赋值
那么是否可以在您无法控制的两种类型之间定义隐式转换运算符(在我的例子中是 PA_Unichar*
和 std::string
),这可能是也可能不是班级类型?
如果不是,有什么解决方法?
In C++ is it possible to define conversion operators which are not class members? I know how to do that for regular operators (such as +), but not for conversion operators.
Here is my use case: I work with a C Library which hands me out a PA_Unichar *
, where the library defines PA_Unichar to be a 16-bit int. It is actually a string coded in UTF-16. I want to convert it to a std::string
coded in UTF-8. I have all the conversion code ready and working, and I am only missing the syntactic sugar that would allow me to write:
PA_Unichar *libOutput = theLibraryFunction();
std::string myString = libOutput;
(usually in one line without the temp variable).
Also worth noting:
I know that
std::string
doesn't define implicit conversion fromchar*
and I know why. The same reason might apply here, but that's beside the point.I do have a
ustring
, subclass ofstd::string
that defines the right conversion operator fromPA_Unichar*
. It works but this means usingustring
variables instead ofstd::string
and that then requires conversion tostd::string
when I use those strings with other libraries. So that doesn't help very much.Using an assignment operator doesn't work as those must be class members.
So is it possible to define implicit conversion operators between two types you don't control (in my case PA_Unichar*
and std::string
), which may or may not be class types?
If not what could be workarounds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
免费功能有什么问题吗?
编辑回答评论:
正如DrPizza所说:其他人都在尝试插入通过用您称为“视觉混乱”的显式转换替换隐式转换而打开的漏洞。
至于临时字符串:等待下一个编译器版本。它可能带有右值引用,并且它的 std::string 实现将在此基础上实现移动语义,从而消除复制。我还没有看到比简单升级到新的编译器版本更便宜的加速代码的方法。
What's wrong with a free function?
Edit answering to the comment:
As DrPizza says: Everybody else is trying to plug the holes opened up by implicit conversions through replacing them with those explicit conversion which you call "visual clutter".
As to the temporary string: Just wait for the next compiler version. It's likely to come with rvalue references and its
std::string
implementation will implement move semantics on top of that, which eliminates the copy. I have yet to see a cheaper way to speedup your code than than by simply upgrading to a new compiler version.我认为您无法定义“全局”转换运算符。标准规定
转换函数
是特殊成员函数
。如果我可以考虑以下语法糖,我会提出以下建议:请注意,此类的多态行为已被破坏。只要您不通过
string*
类型的指针创建它的对象,您就处于安全的一面!所以,这段代码是完美的:正如之前所说,下面的代码被破坏了!
I don't think you can define "global" conversion operators. The standards say that
conversion functions
arespecial member functions
. I would propse the following if I could consider the following syntax sugar:Be aware that polymorphic behavior of this class is broken. As long as you don't create an object of it through a pointer of type
string*
though, you are in the safe-side! So, this code is perfect:As said before, the following code is broken!
无论如何,隐式转换是魔鬼。通过转换函数调用使其明确。
Implicit conversions are the devil, anyway. Make it explicit with a converting function call.
不,你不能。作为替代方案,您可以做的是在目标类中创建一个转换构造函数(不是您的情况,因为您想要转换为 std::string - 除非您派生它)。但我同意其他答案,我认为在这种情况下不建议隐式转换 - 特别是因为您不是从对象转换而是从指针转换。最好有一个免费的函数,你的代码会更容易理解,下一个继承代码的程序员一定会感谢你。
No, you can't. What you could do as an alternative is to create a conversion constructor in the target class (not your case, as you want to convert to std::string - unless you derive it). But I agree to the other answers, I think an implicit conversion is not recommended in this case - especially because you're not converting from an object but from a pointer. Better to have a free function, your code will be easier to understand and the next programmer to inherit the code will for sure thank you.