C++用户定义的没有类的转换运算符?

发布于 2024-08-08 08:01:53 字数 994 浏览 5 评论 0原文

在 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 from char* and I know why. The same reason might apply here, but that's beside the point.

  • I do have a ustring, subclass of std::string that defines the right conversion operator from PA_Unichar*. It works but this means using ustring variables instead of std::string and that then requires conversion to std::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木落 2024-08-15 08:01:53

免费功能有什么问题吗?

std::string convert(PA_Unichar *libOutput);

std::string myString = convert(theLibraryFunction());

编辑回答评论:

正如DrPizza所说:其他人都在尝试插入通过用您称为“视觉混乱”的显式转换替换隐式转换而打开的漏洞。

至于临时字符串:等待下一个编译器版本。它可能带有右值引用,并且它的 std::string 实现将在此基础上实现移动语义,从而消除复制。我还没有看到比简单升级到新的编译器版本更便宜的加速代码的方法。

What's wrong with a free function?

std::string convert(PA_Unichar *libOutput);

std::string myString = convert(theLibraryFunction());

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.

星軌x 2024-08-15 08:01:53

我认为您无法定义“全局”转换运算符。标准规定转换函数特殊成员函数。如果我可以考虑以下语法糖,我会提出以下建议:

struct mystring : public string
{
    mystring(PA_Unichar * utf16_string)
    {
        // All string functionality is present in your mystring.
        // All what you have to do is to write the conversion process.
        string::operator=("Hello World!");
        string::push_back('!');
        // any string operation...
    }
};

请注意,此类的多态行为已被破坏。只要您不通过 string* 类型的指针创建它的对象,您就处于安全的一面!所以,这段代码是完美的:

mystring str(....);

正如之前所说,下面的代码被破坏了!

string* str = new mystring(....);
....
delete str; // only deleting "string", but not "mystring" part of the object
// std::string doesn't define virtual destructor

I don't think you can define "global" conversion operators. The standards say that conversion functions are special member functions. I would propse the following if I could consider the following syntax sugar:

struct mystring : public string
{
    mystring(PA_Unichar * utf16_string)
    {
        // All string functionality is present in your mystring.
        // All what you have to do is to write the conversion process.
        string::operator=("Hello World!");
        string::push_back('!');
        // any string operation...
    }
};

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:

mystring str(....);

As said before, the following code is broken!

string* str = new mystring(....);
....
delete str; // only deleting "string", but not "mystring" part of the object
// std::string doesn't define virtual destructor
眼眸里的快感 2024-08-15 08:01:53

无论如何,隐式转换是魔鬼。通过转换函数调用使其明确。

Implicit conversions are the devil, anyway. Make it explicit with a converting function call.

2024-08-15 08:01:53

不,你不能。作为替代方案,您可以做的是在目标类中创建一个转换构造函数(不是您的情况,因为您想要转换为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文