在函数重载中使用标签

发布于 2024-12-25 01:50:40 字数 519 浏览 2 评论 0原文

为什么 X 中的方法测试不明确,如何解决这个问题?

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> {};

int main(int argc, const char *argv[])
{
    X x;
    x.test(A());
    return 0;
}

编译错误:

In function ‘int main(int, const char**)’:
error: request for member ‘test’ is ambiguous
error: candidates are: void I<T>::test(T) [with T = B]
error:                 void I<T>::test(T) [with T = A]

Why is method test in X ambiguous, and how can this be fixed?

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> {};

int main(int argc, const char *argv[])
{
    X x;
    x.test(A());
    return 0;
}

Compile error:

In function ‘int main(int, const char**)’:
error: request for member ‘test’ is ambiguous
error: candidates are: void I<T>::test(T) [with T = B]
error:                 void I<T>::test(T) [with T = A]

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

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

发布评论

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

评论(3

没︽人懂的悲伤 2025-01-01 01:50:40

test 是不明确的,因为它是 X 的两个基类的成员。虽然两个函数并不匹配,但名称匹配。

使用显式转发修复:

struct X : public I<A>, I<B> {
    template <typename T>
    void test(T t) { I<T>::test(t); }
};

test is ambiguous because it is a member of two base classes of X. While not both functions match, the name matches.

Fix with an explicit forward:

struct X : public I<A>, I<B> {
    template <typename T>
    void test(T t) { I<T>::test(t); }
};
岁月静好 2025-01-01 01:50:40

使用 using:

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> 
{
    using I<A>::test;
    using I<B>::test;
};

int main(int argc, const char *argv[])
{
    X x;
    x.test(A());
    return 0;
}

gcc 错误 request for member 'test' is ambigacy 在这里并不是最好的,我们可以更好地看到 clang: member 'test' found in 错误的含义不同类型的多个基类

Use using:

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> 
{
    using I<A>::test;
    using I<B>::test;
};

int main(int argc, const char *argv[])
{
    X x;
    x.test(A());
    return 0;
}

The gcc error request for member 'test' is ambiguous is not really the best here, we can see better what is meant with the error of clang: member 'test' found in multiple base classes of different types

不顾 2025-01-01 01:50:40

由于 X 乘法继承自 II,因此对 test() 的调用为模糊的。您可以这样做来显式声明您所指的是哪个父级:

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> {};

int main(int argc, const char *argv[])
{
    X x;
    x.I<A>::test(A());
    return 0;
}

Because X multiply inherits from I<A> and I<B>, the call to test() is ambiguous. You can do this to explicitly declare which parent you are referring to:

struct A{};
struct B{};

template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> {};

int main(int argc, const char *argv[])
{
    X x;
    x.I<A>::test(A());
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文