VC2017 错误匹配模板类参数

VC2017 error matching template class parameter

本文关键字:参数 错误 VC2017      更新时间:2023-10-16

>我在VS2013中使用了一个C++模板函数,没有任何问题。但是当我升级到VS2017时,VC编译器抱怨它无法匹配参数列表。任何人都可以帮助我如何修复代码?

演示此处问题的简化代码片段:

#include "stdafx.h"
#include <functional>
#include <memory>
class FS
{
};
typedef std::shared_ptr<FS> FSPtr;
class FSM
{
public:
    FSM() : m_pFs(new FS()) {}
    template <typename CALLABLE, typename... ARGS>
    typename std::enable_if<std::is_same<bool, std::result_of_t<CALLABLE(ARGS&&...)>>::value, std::result_of_t<CALLABLE(ARGS&&...)>>::type
        All(CALLABLE fn, ARGS&&... args) const        // line 21
    {
        std::function<bool()> rFunc = std::bind(fn, m_pFs, args...);
        bool bSuccess = rFunc();
        return bSuccess;
    }
private:
    FSPtr m_pFs;
};
class SFF
{
public:
    SFF() : m_pFsm(new FSM()) {}
    bool VF(FSPtr pFs)
    {
        return nullptr != pFs;
    }
    bool Do()
    {
        return m_pFsm->All(std::bind(&SFF::VF, this, std::placeholders::_1));        // line 41
    }
    bool TF(FSPtr pFs, int n)
    {
        return nullptr != pFs && 0 != n;
    }
    bool Do1(int n)
    {
        return m_pFsm->All(std::bind(&SFF::TF, this, std::placeholders::_1, std::placeholders::_2), n);        // line 49
    }
private:
    std::shared_ptr<FSM> m_pFsm;
};

int _tmain(int argc, _TCHAR* argv[])
{
    SFF oSff;
    bool bOk1 = oSff.Do();
    bool bOk2 = oSff.Do1(4);
    int rc =  (bOk1 && bOk2) ? 0 : 1;
    return rc;
}

VS2017 VC编译器输出的错误是:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>ConsoleApplication1.cpp
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.11.25503includeutility(486): error C2338: tuple index out of bounds
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.11.25503includefunctional(887): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
1>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.11.25503includetuple(793): note: see reference to function template instantiation 'const tuple_element<_Index,_Tuple>::type &&std::get(const std::tuple<_Rest...> &&) noexcept' being compiled
1>        with
1>        [
1>            _Tuple=std::tuple<_Rest...>
1>        ]
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(41): note: see reference to class template instantiation 'std::result_of<std::_Binder<std::_Unforced,bool (__thiscall SFF::* )(FSPtr),SFF *,const std::_Ph<1> &> (void)>' being compiled
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(21): note: while compiling class template member function 'std::enable_if<std::is_same<bool,result_of<_Ty>::type>::value,result_of<_Ty>::type>::type FSM::All(CALLABLE,ARGS &&...) const'
1>        with
1>        [
1>            _Ty=CALLABLE (ARGS &&...)
1>        ]
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(41): error C2672: 'FSM::All': no matching overloaded function found
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(41): error C2893: Failed to specialize function template 'std::enable_if<std::is_same<bool,result_of<_Ty>::type>::value,result_of<_Ty>::type>::type FSM::All(CALLABLE,ARGS &&...) const'
1>        with
1>        [
1>            _Ty=CALLABLE (ARGS &&...)
1>        ]
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(41): note: With the following template arguments:
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(41): note: 'CALLABLE=std::_Binder<std::_Unforced,bool (__thiscall SFF::* )(FSPtr),SFF *,const std::_Ph<1> &>'
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(41): note: 'ARGS={}'
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(49): error C2672: 'FSM::All': no matching overloaded function found
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(49): error C2893: Failed to specialize function template 'std::enable_if<std::is_same<bool,result_of<_Ty>::type>::value,result_of<_Ty>::type>::type FSM::All(CALLABLE,ARGS &&...) const'
1>        with
1>        [
1>            _Ty=CALLABLE (ARGS &&...)
1>        ]
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(49): note: With the following template arguments:
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(49): note: 'CALLABLE=std::_Binder<std::_Unforced,bool (__thiscall SFF::* )(FSPtr,int),SFF *,const std::_Ph<1> &,const std::_Ph<2> &>'
1>c:userss.chansourcereposconsoleapplication1consoleapplication1consoleapplication1.cpp(49): note: 'ARGS={int &}'
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

任何帮助都非常感谢。

显然fn(CALLABLE(在FSM::All()中应该被称为fn(m_pFs, args...),而不是fn(args...)

所以你的SFINAE是错误的:

  • std::result_of_t<CALLABLE(ARGS&&...)>缺少m_pFs参数:

  • std::result_of_t<CALLABLE(FSPtr, ARGS&&...)>

如果添加FSPtr它应该可以工作。但请记住,result_of已被弃用。只需使用尾随返回类型即可实现相同的效果:

template <typename CALLABLE, typename... ARGS>
auto All(CALLABLE fn, ARGS&&... args)
    -> std::enable_if_t<std::is_same_v<bool, decltype(fn(std::declval<FSPtr>(), args...))>, bool>
{

另请注意,std::bind返回一个 lambda。从中创建std::function将是低效的。最好按原样使用返回的类型:

    auto rFunc = std::bind(fn, m_pFs, args...); // no need to cast to std::function
    rFunc();
<</div> div class="answers">

代码无法编译的原因如下:

bool Do()
{
    return m_pFsm->All(std::bind(&SFF::VF, this, std::placeholders::_1));
}

在这里,您使用包装函数All来调用 VF 函数。但:1(All函数应该获取函数参数:

template <typename CALLABLE, typename... ARGS>
    typename std::enable_if<std::is_same<bool, std::result_of_t<CALLABLE(ARGS&&...)>>::value, std::result_of_t<CALLABLE(ARGS&&...)>>::type
    All(CALLABLE fn, ARGS&&... args) const        // line 21
{
    std::function<bool()> rFunc = std::bind(fn, m_pFs, args...);
    bool bSuccess = rFunc();
    return bSuccess;
}

Args这里应该代表FSPtr类型的单个参数,请参阅 SFF::VF 的签名。

所以正确的代码应该是:

return m_pFsm->All(std::bind(&SFF::VF, this, std::placeholders::_1), somethingOfFSPtrType);