私は次のような小さなプログラムを持っています:
#include <iostream>
template <typename T>
class X{
public:
bool something(){
return true;
}
};
class A: public X<A>{
};
class B: public A, public X<B>{
};
template <typename T>
bool use(T &t)
{
return t.something();
}
int main()
{
B b;
std::cout << "use returned: " << use(b);
}
の2つの可能なバージョンのsomething()
どちらを選択する必要があるかについてあいまいさがあるため、これはコンパイルされません。
In instantiation of 'bool use(T&) [with T = B]': 30:43: required from here 22:20: error: request for member 'something' is ambiguous 6:14: note: candidates are: bool X<T>::something() [with T = B] 6:14: note: bool X<T>::something() [with T = A] In function 'bool use(T&) [with T = B]': 23:1: warning: control reaches end of non-void function [-Wreturn-type]
私の質問は、編集できる場所が本体だけである場合、このあいまいさをどのように解決できるかということuse()
です。
はい。たとえば、something
(godbolt)への呼び出しを修飾できます。
template <typename T>
bool use(T &t)
{
return t.A::something();
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加