멤버 함수가 존재하고 이 아닌지 확인하려면 어떻게해야합니까?이 상속되지 않았습니까? SFINAE에 멤버 함수가 있고 상속되지 않았는지 확인하십시오.
나는 다음과 같은 예를 들어 모호성을 해결하려면이 필요합니다유형 중 하나가있는 foo()
또는 bar()
멤버 함수. Caller
은 주어진 유형에 대해 존재하는 call
입니다. 그러나 DerivedWithBar
은 BaseWithFoo
에서 foo()
을 상속하지만 자신의 bar()
을 정의합니다. 따라서 Caller
은 호출 할 함수를 알지 못합니다.
상속 된 foo
우선 순위를 상속 된 bar()
보다 우선해야하지만 멤버 함수가 상속되었는지 여부를 확인하는 방법을 모르겠습니다.
#include <iostream>
struct BaseWithFoo
{
template <typename T> void foo(T&&){std::cout << "Base::foo" << std::endl;}
};
struct DerivedWithBar : public BaseWithFoo
{
template <typename T> void bar(T&&){std::cout << "DerivedWithBar::bar" << std::endl;}
};
struct DerivedWithFoo : public BaseWithFoo
{
template <typename T> void foo(T&&){std::cout << "DerivedWithFoo::foo" << std::endl;}
};
struct EmptyDerived : public BaseWithFoo {};
struct BaseWithBar
{
template <typename T> void bar(T&&){std::cout << "BaseWithBar::bar" << std::endl;}
};
struct Caller
{
template <typename T>
auto call(T&& x) -> decltype(x.foo(*this), void())
{
x.foo(*this);
}
template <typename T>
auto call(T&& x) -> decltype(x.bar(*this), void())
{
x.bar(*this);
}
};
int main()
{
Caller c;
c.call(BaseWithFoo());
c.call(DerivedWithFoo());
c.call(DerivedWithBar());
c.call(EmptyDerived());
c.call(BaseWithBar());
}
원하는 출력 :
Base::foo
DerivedWithFoo::foo
DerivedWithBar::bar
Base::foo
BaseWithBar::bar
테스트 할 함수의 서명을 알고 있습니까 ('void U :: foo (Caller &)')? – Jarod42
@ Jarod42 서명은 작성한대로 템플릿 기능이므로 generic이 필요합니다 –