Search code examples
c++templatesvisual-studio-2017

VC2017 error matching template class parameter


I have a C++ template function being used in VS2013 without any problem. But when I upgrade to VS2017 the VC compiler complains it cannot match the argument list. Anyone can help me how to fix the code?

A simplified code snippet that demonstrates the problem here:

#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;
}

And the errors VS2017 VC compiler output is:

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>ConsoleApplication1.cpp
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\utility(486): error C2338: tuple index out of bounds
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\functional(887): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\tuple(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:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.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:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.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:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41): error C2672: 'FSM::All': no matching overloaded function found
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.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:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41): note: With the following template arguments:
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41): note: 'CALLABLE=std::_Binder<std::_Unforced,bool (__thiscall SFF::* )(FSPtr),SFF *,const std::_Ph<1> &>'
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41): note: 'ARGS={}'
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49): error C2672: 'FSM::All': no matching overloaded function found
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.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:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49): note: With the following template arguments:
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49): note: 'CALLABLE=std::_Binder<std::_Unforced,bool (__thiscall SFF::* )(FSPtr,int),SFF *,const std::_Ph<1> &,const std::_Ph<2> &>'
1>c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49): note: 'ARGS={int &}'
1>Done building project "ConsoleApplication1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any help is much appreciated.


Solution

  • Apparently fn (CALLABLE) in FSM::All() is supposed to be called as fn(m_pFs, args...), not fn(args...)

    So your SFINAE is wrong:

    • std::result_of_t<CALLABLE(ARGS&&...)> is missing the m_pFs argument:

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

    If you add FSPtr it should work. But keep in mind that result_of is deprecated. You can achieve the same effect simply with a trailing return type:

    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>
    {
    

    Note also that std::bind returns a lambda. Creating an std::function from that will be inefficient. Better to just use the returned type as-is:

        auto rFunc = std::bind(fn, m_pFs, args...); // no need to cast to std::function
        rFunc();