c++ - 在函数重载中使用标签

标签 c++ generic-programming

为什么 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]

最佳答案

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

用显式转发修复:

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

关于c++ - 在函数重载中使用标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8742699/

相关文章:

c++ - 有没有办法记住(存储)decltype 的返回类型?

java - 为批量任务实现通用进度跟踪器

reflection - Go 中的泛型编程。避免硬编码类型断言

c++ - cppitertools:如何结合 iter::enumerate 和 iter::filter?

c++ - linux C++ 通知文件类型创建

c++ - 如何实现指向其他多类型 vector 的 C++ vector ?

c++ - 指向具有不同参数的成员函数的指针的容器

c++ - 具有混合类型和非类型可变参数的模板模板参数

php - 从 php 检索值?

php - Proc_open() c++/python