gpt4 book ai didi

c++ - 在 VS2013 中使用可变参数模板时为 "ambiguous call to overloaded function"

转载 作者:行者123 更新时间:2023-12-03 12:20:23 25 4
gpt4 key购买 nike

给定以下代码:

        template <typename T>
bool TryQueryInterface(
IUnknown* in_toQuery,
REFIID riid,
void **ppvObject,
ComObject* in_parent,
HRESULT out_result)
{
if (InterfaceProperties<T>::GetIID() == riid)
{
void *underlying;
HRESULT result = in_toQuery->QueryInterface(riid, &underlying);
if (SUCCEEDED(result))
{
*ppvObject = new typename InterfaceProperties<T>::WrapperClass(
*this,
(T*)underlying,
in_parent);
}

return true;
}

return false;
}

template <typename T, typename... Interfaces>
bool TryQueryInterfaces(
IUnknown* in_toQuery,
REFIID riid,
void **ppvObject,
ComObject* in_parent,
HRESULT out_result)
{
return TryQueryInterface<T>(in_toQuery, riid, ppvObject, in_parent, out_result) ||
TryQueryInterfaces<Interfaces...>(in_toQuery, riid, ppvObject, in_parent, out_result);
}

template <typename T>
bool TryQueryInterfaces(
IUnknown* in_toQuery,
REFIID riid,
void **ppvObject,
ComObject* in_parent,
HRESULT out_result)
{
return TryQueryInterface<T>(in_toQuery, riid, ppvObject, in_parent, out_result);
}

我收到以下错误:

    error C2668: 'TryQueryInterfaces' : ambiguous call to overloaded function
TryQueryInterfaces<ITrusteeGroupAdmin>(IUnknown *,const IID &,void **,ComObject *,HRESULT)'
or TryQueryInterfaces<ITrusteeGroupAdmin,>(IUnknown *,const IID &,void **,ComObject *,HRESULT)'
while trying to match the argument list '(IUnknown *, const IID, void **, ComObject *, HRESULT)'
see reference to function template instantiation 'bool TryQueryInterfaces<ITrusteeAdmin,ITrusteeGroupAdmin>(IUnknown *,const IID &,void **,ComObject *,HRESULT)' being compiled

我在这里错过了什么?如何为递归构建明确的基本情况?

最佳答案

这是不明确的,因为参数包 Interfaces... 可能是空的。确保你至少接受一个参数加上一些额外的(可能为零)参数。将第二种方法改为:

template <typename T, typename Interface, typename... Interfaces>
bool TryQueryInterfaces(
IUnknown* in_toQuery,
REFIID riid,
void **ppvObject,
ComObject* in_parent,
HRESULT out_result)
{
return TryQueryInterface<T>(in_toQuery, riid, ppvObject, in_parent, out_result) ||
TryQueryInterfaces<Interface, Interfaces...>(in_toQuery, riid, ppvObject, in_parent, out_result);
}

关于c++ - 在 VS2013 中使用可变参数模板时为 "ambiguous call to overloaded function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24875080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com