Call to base member function template ambiguity in C++
I'm trying to implement a DependencyInjectable behavior, and classes that derive from this behavior are able to be injected with the appropriate dependencies upon instantiation.
Below I've tried to extract and condense a sample of code from a much larger project to illustrate the problem I am having. It's a little lengthy, and I apologize for that.
#include <tuple>
#include <type_traits>
template <typename, typename> struct tuple_has_type;
template <typename T> struct tuple_has_type<T, std::tuple<>> : std::false_type { };
template <typename T, typename U, typename ... Args> struct tuple_has_type<T, std::tuple<U, Args ...>> : tuple_has_type<T, std::tuple<Args ...>> { };
template <typename T, typename ... Args> struct tuple_has_type<T, std::tuple<T, Args ...>> : std::true_type { };
template<typename ... Dependencies>
class DependencyInjectable
{
private:
template<int index, typename ... Args>
struct assign_dependencies
{
void operator () (std::tuple<Dependencies ...> &lhs, std::tuple<Args ...> &&rhs)
{
typedef typename std::tuple_element<index, std::tuple<Dependencies ...>>::type T;
std::get<T>(lhs) = std::get<T>(rhs);
assign_dependencies<index - 1, Args ...> { } (lhs, std::forward<std::tuple<Args ...>>(rhs));
}
};
template<typename ... Args>
struct assign_dependencies<0, Args ...>
{
void operator() (std::tuple<Dependencies ...> &lhs, std::tuple<Args ...> &&rhs)
{
typedef typename std::tuple_element<0, std::tuple<Dependencies ...>>::type T;
std::get<T>(lhs) = std::get<T>(rhs);
}
};
public:
template<typename ... Args>
DependencyInjectable(Args ... dependencies)
{
setDependencies(std::forward<Args>(dependencies) ...);
}
virtual ~DependencyInjectable(void) { }
template<typename T> auto getDependency(void) const ->
typename std::enable_if<tuple_has_type<T, std::tuple<Dependencies ...>>::value, T>::type
{
return std::get<T>(m_dependencies);
}
template<typename T, typename U = typename std::decay<T>::type>
auto setDependency(T &&dependency) ->
typename std::enable_if<tuple_has_type<U, std::tuple<Dependencies ...>>::value, void>::type
{
std::get<U>(m_dependencies) = dependency;
}
template<typename ... Args>
void setDependencies(Args ... dependencies)
{
constexpr auto size = std::tuple_size<std::tuple<Dependencies ...>>::value;
static_assert(size > 0, "List of dependencies must be specified.");
assign_dependencies<size - 1, Args ...> { } (m_dependencies, std::forward_as_tuple(
std::forward<Args>(dependencies) ...));
}
private:
std::tuple<Dependencies ...> m_dependencies; // an std::tuple containing injection dependencies
};
class DependencyOfBase { };
class DependencyOfDerived { };
class Base
: public DependencyInjectable<DependencyOfBase *>
{
public:
Base(DependencyOfBase *pDependencyOfBase)
: DependencyInjectable<DependencyOfBase *>(pDependencyOfBase)
{
}
virtual ~Base(void) { }
};
class Derived
: public Base, public DependencyInjectable<DependencyOfDerived *>
{
public:
using Base::getDependency;
using DependencyInjectable<DependencyOfDerived *>::getDependency;
Derived(DependencyOfBase *pDependencyOfBase, DependencyOfDerived *pDependencyOfDerived)
: Base(pDependencyOfBase),
DependencyInjectable<DependencyOfDerived *>(pDependencyOfDerived)
{
}
virtual ~Derived(void) { }
};
int main(int argc, char **argv)
{
DependencyOfBase dependencyOfBase;
DependencyOfDerived dependencyOfDerived;
Base base(&dependencyOfBase);
Derived derived(&dependencyOfBase, &dependencyOfDerived);
auto *pDependencyOfBase = base.getDependency<DependencyOfBase *>();
auto *pDependencyOfDerived = derived.getDependency<DependencyOfDerived *>();
return (pDependencyOfBase != nullptr && pDependencyOfDerived != nullptr) ? 0 : 1;
}
In particular, I use std::tuple to store dependencies within the DependencyInjectable class, and I am trying to use enable_if to disable the getDependency() function when the tuple does not contain the requested type. In main, I make a call to an instance of Derived's getDependency() function and I specify "DependencyOfDerived *". This works as long as I specify
using Base::getDependency;
using DependencyInjectable<DependencyOfDerived *>::getDependency;
in the Derived class, but I would have thought that the enable_if would have disabled the getDependency() function in the base for the "DependencyOfDerived *" template parameter since the Base tuple does not contain that type. What am I missing here?
If I comment the using statements, this is the following output I get from gcc:
In function 'int main(int, char**)': 116:42: error: request for member 'getDependency' is ambiguous 45:31: note: candidates are: template typename std::enable_if >::value, T>::type DependencyInjectable::getDependency() const [with T = T; Dependencies = {DependencyOfDerived*}] 45:31: note: template typename std::enable_if >::value, T>::type DependencyInjectable::getDependency() const [with T = T; Dependencies = {DependencyOfBase*}] 116:76: error: expected primary-expression before '*' token 116:77: error: expected primary-expression before '>' token 116:79: error: expected primary-expression before ')' token
链接地址: http://www.djcxy.com/p/90142.html上一篇: 如果以std :: is为基础的话
下一篇: 在C ++中调用基本成员函数模板模糊性