简体   繁体   中英

Error in VS2013 when attempting to partially specialize a class template with another class template

Given the following class and function templates:

template <typename WrappedType, ParameterType ParamType, bool IsOutputParameter>
    class WrappedParameter; // Definition left out for brevity

template <typename T>
struct ParameterUnwrapper
{
    static T UnwrapParameter(const T& in_param)
    {
        return in_param;
    }
};

template <typename T, ParameterType ParamType, bool IsOutputParameter>
struct ParameterUnwrapper<WrappedParameter<T, ParamType, IsOutputParameter>>
{
    static T UnwrapParameter(const WrappedParameter<T, ParamType, IsOutputParameter>& in_param)
    {
        return in_param.GetWrapped();
    }
};

template <typename T>
T UnwrapParameter(T in_param)
{
    return Impl::ParameterUnwrapper<T>::UnwrapParameter(in_param);
}

template <typename T>
Impl::WrappedParameter<T, Impl::POINTER_PARAMETER, true> WrapOutputPointerParameter(T in_param)
{
    return Impl::WrappedParameter<T, Impl::POINTER_PARAMETER, true>(in_param);
}

template <typename MemFunc, typename ...Args>
HRESULT ExecuteAndLog(
    MemFunc in_memberFunction,
    const std::string& in_methodName,
    Args... args) //-> decltype((m_wrapped->*in_memberFunction)(UnwrapParameter(args)...))
{
    return ExecuteFunctorAndLog(
        [&]() { return (m_wrapped->*in_memberFunction)(UnwrapParameter(args)...); },
        in_methodName,
        args...);
}

The following call: (The ExecuteAndLog)

HRESULT STDMETHODCALLTYPE AccessorWrapper::AddRefAccessor(
    HACCESSOR hAccessor,
    DBREFCOUNT *pcRefCount)
{
    return ExecuteAndLog(
        &IAccessor::AddRefAccessor,
        "AddRefAccessor",
        hAccessor,
        WrapOutputPointerParameter(pcRefCount));
}

Gives me errors:

error C2664: 'HRESULT (HACCESSOR,DBREFCOUNT *)' : cannot convert argument 2 from 'Impl::WrappedParameter<DBREFCOUNT *,POINTER_PARAMETER,true>' to 'DBREFCOUNT *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
see reference to function template instantiation 'ExecuteAndLog<HRESULT(__stdcall IAccessor::* )(HACCESSOR,DBREFCOUNT *),HACCESSOR, Impl::WrappedParameter<DBREFCOUNT *,POINTER_PARAMETER,true>>(MemFunc,const std::string &,HACCESSOR,Impl::WrappedParameter<DBREFCOUNT *,POINTER_PARAMETER,true>)' being compiled
with
[
    MemFunc=HRESULT (__stdcall IAccessor::* )(HACCESSOR,DBREFCOUNT *)
]
see reference to function template instantiation 'ExecuteAndLog<HRESULT(__stdcall IAccessor::* )(HACCESSOR,DBREFCOUNT *),HACCESSOR,Impl::WrappedParameter<DBREFCOUNT *,POINTER_PARAMETER,true>>(MemFunc,const std::string &,HACCESSOR,Impl::WrappedParameter<DBREFCOUNT *,POINTER_PARAMETER,true>)' being compiled
with
[
    MemFunc=HRESULT (__stdcall IAccessor::* )(HACCESSOR,DBREFCOUNT *)
]

I think I've messed up the partial specialization of ParameterUnwrapper (or my approach is just wrong). Any advice?

More information:

Impl is a nested namespace (alongside the namespace all the provided templates except ExecuteAndLog are in)

m_wrapped is of type IAccessor* (the COM interface) in this case.

        enum ParameterType
        {
            POINTER_PARAMETER,

            ARRAY_PARAMETER
        };

UPDATE: Here's a self contained example: http://codepad.org/lwTzVImb

The error I get in VS2013 for this one is:

error C2664: 'int (int,int **,size_t *)' : cannot convert argument 2 from 'WrappedParameter<int **,ARRAY_PARAMETER,true>' to 'int **'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
see reference to function template instantiation 'int ExecuteAndLog<int(__thiscall A::* )(int,int **,size_t *),int,WrappedParameter<int **,ARRAY_PARAMETER,true>,size_t*>(MemFunc,const std::string &,int,WrappedParameter<int **,ARRAY_PARAMETER,true>,size_t *)' being compiled
with
[
    MemFunc=int (__thiscall A::* )(int,int **,size_t *)
]

I figured it out!

The issue was the return type of UnwrapParameter. Once I changed the declaration of it to

    template <typename T>
    auto UnwrapParameter(T in_param) -> decltype(Impl::ParameterUnwrapper<T>::UnwrapParameter(in_param))

It compiled. Shame the compiler didn't complain about the definition of that function, rather than trusting its declared return value.

I have some other problems right now but at least I've made progress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM