在 C++ 中使隐式转换运算符优先于另一个运算符;

发布于 2024-12-03 04:15:52 字数 838 浏览 3 评论 0原文

我想更喜欢某个隐式转换序列而不是另一个。我有以下(大大简化的)类和函数:

class Whatever {...}

template <class T>
class ref
{
    public:

        operator T* ()
        {
            return object;
        }

        operator T& ()
        {
            return *object;
        }

        T* object;
        ...
};

void f (Whatever*)
{
    cout << "f (Whatever*)" << endl;
}

void f (Whatever&)
{
    cout << "f (Whatever&") << endl;
}

int main (int argc, char* argv[])
{
    ref<Whatever> whatever = ...;
    f(whatever);
}

当我有一个 ref 对象并且对 f 进行不明确的调用时,我希望编译器选择涉及 T& 的类 和函数。但在其他明确的情况下,我希望隐式转换保持不变。

到目前为止,我已经尝试引入一个中间类,其中 ref 可以隐式转换为,并且具有到 T* 的隐式转换运算符,因此转换序列会更长。不幸的是,它没有在明确的情况下认识到它确实可以转换为 T*。当中间类有一个(n隐式)构造函数时,也会发生同样的事情。毫不奇怪,这个版本与 ref 完全无关。

我还尝试制作隐式转换运算符模板之一,结果相同。

I would like to prefer a certain implicit conversion sequence over another. I have the following (greatly simplified) class and functions:

class Whatever {...}

template <class T>
class ref
{
    public:

        operator T* ()
        {
            return object;
        }

        operator T& ()
        {
            return *object;
        }

        T* object;
        ...
};

void f (Whatever*)
{
    cout << "f (Whatever*)" << endl;
}

void f (Whatever&)
{
    cout << "f (Whatever&") << endl;
}

int main (int argc, char* argv[])
{
    ref<Whatever> whatever = ...;
    f(whatever);
}

When I have a ref object and I am making an ambiguous call to f, I would like the compiler to choose the one involving T&. But in other unambiguous cases I wish the implicit conversion to remain the same.

So far I have tried introducing an intermediate class which ref is implicitly convertible to, and which has an implicit conversion operator to T*, so the conversion sequence would be longer. Unfortunately it did not recognize in unambiguous cases that it is indeed convertible to T*. Same thing happened when the intermediate class had a(n implicit) constructor. It's no wonder, this version was completely unrelated to ref.

I also tried making one of the implicit conversion operators template, same result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

困倦 2024-12-10 04:15:52

这两种转换之间没有“排名”;两者都同样好,因此过载是不明确的。这是语言的核心部分,你无法改变。

但是,您可以通过显式转换来指定所需的重载:

f((Whatever&) whatever);

There's no "ranking" among the two conversions; both are equally good and hence the overload is ambiguous. That's a core part of the language that you cannot change.

However, you can just specify which overload you want by making the conversion explicit:

f((Whatever&) whatever);
北渚 2024-12-10 04:15:52

简单:定义 void f(const ref&),它将胜过其他需要转换的内容。

Simple: define void f(const ref<Whatever>&), it will trump the others which require a conversion.

安静被遗忘 2024-12-10 04:15:52

执行隐式转换时,仅应用一种用户定义的转换函数。如果没有定义的转换函数,编译器不会查找对象可以转换为的中间类型。

Only one user-defined conversion function is applied when performing implicit conversions. If there is no defined conversion function, the compiler does not look for intermediate types into which an object can be converted.

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