Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VC2017匹配模板类参数时出错 我在VS2013中使用了C++模板函数,没有任何问题。但是当我升级到VS2017时,VC编译器抱怨它与参数列表不匹配。有人能帮我修复代码吗_C++_Templates_Visual Studio 2017 - Fatal编程技术网

VC2017匹配模板类参数时出错 我在VS2013中使用了C++模板函数,没有任何问题。但是当我升级到VS2017时,VC编译器抱怨它与参数列表不匹配。有人能帮我修复代码吗

VC2017匹配模板类参数时出错 我在VS2013中使用了C++模板函数,没有任何问题。但是当我升级到VS2017时,VC编译器抱怨它与参数列表不匹配。有人能帮我修复代码吗,c++,templates,visual-studio-2017,C++,Templates,Visual Studio 2017,一个简化的代码段演示了此处的问题: #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... AR

一个简化的代码段演示了此处的问题:

#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;
}
#包括“stdafx.h”
#包括
#包括
FS类
{
};
typedef std::shared_ptr FSPtr;
类有限状态机
{
公众:
FSM():m_pFs(新的FS()){}
模板
typename std::enable_if::type
所有(可调用fn、ARGS&…ARGS)常量//第21行
{
std::function rFunc=std::bind(fn,m_pFs,args;
bool bSuccess=rFunc();
返回b成功;
}
私人:
FSPtr m_pFs;
};
类SFF
{
公众:
SFF():m_pFsm(新的FSM()){}
bool VF(FSPtr pFs)
{
返回null ptr!=pFs;
}
布尔多
{
返回m_pFsm->All(std::bind(&SFF::VF,this,std::placeholders::_1));//第41行
}
bool TF(FSPtr pFs,int n)
{
返回nullptr!=pFs&&0!=n;
}
布尔多尔1(国际北)
{
返回m_pFsm->All(std::bind(&SFF::TF,this,std::placeholders::_1,std::placeholders::_2),n);//第49行
}
私人:
std::共享的ptr 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;
返回rc;
}
VS2017 VC编译器输出的错误为:

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 ==========
1>----构建已启动:项目:控制台应用程序1,配置:调试Win32------
1> 控制台应用程序1.cpp
1> c:\program files(x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\utility(486):错误C2338:元组索引超出范围
1> c:\program files(x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\functional(887):注意:请参阅正在编译的类模板实例化“std::tuple\u元素”
1> c:\program files(x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.11.25503\include\tuple(793):注意:请参阅正在编译的函数模板实例化“const tuple\u element::type&&std::get(const std::tuple&&noexcept”
1> 与
1>        [
1> \u Tuple=std::Tuple
1>        ]
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41):注意:请参阅正在编译的类模板实例化“std::result\u of”的参考
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(21):注意:在编译类模板成员函数“std::enable_if::type FSM::All(CALLABLE,ARGS&&…)const”时
1> 与
1>        [
1> _Ty=可调用(参数和…)
1>        ]
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41):错误C2672:'FSM::All':未找到匹配的重载函数
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41):错误C2893:未能专门化函数模板“std::enable_if::type FSM::All(可调用、参数和…)const”
1> 与
1>        [
1> _Ty=可调用(参数和…)
1>        ]
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41):注意:使用以下模板参数:
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41):注意:“CALLABLE=std::\u Binder”
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(41):注意:“ARGS={}”
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49):错误C2672:'FSM::All':未找到匹配的重载函数
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49):错误C2893:未能专门化函数模板“std::enable_if::type FSM::All(可调用、参数和…)const”
1> 与
1>        [
1> _Ty=可调用(参数和…)
1>        ]
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49):注意:使用以下模板参数:
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49):注意:“CALLABLE=std::\u Binder”
1> c:\users\s.chan\source\repos\consoleapplication1\consoleapplication1\consoleapplication1.cpp(49):注意:“ARGS={int&}”
1> 已完成生成项目“ConsoleApplication1.vcxproj”--失败。
======生成:0成功,1失败,0最新,0跳过==========

非常感谢您的帮助。

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

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

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

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

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

所以你的SFINAE是错误的:

  • std::result\u of \u t
    缺少
    m\u pFs
    参数:

  • std::测试结果

如果添加
FSPtr
,它应该可以工作。但请记住,的
result\u是不推荐的。只需使用尾随返回类型即可实现相同的效果:

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

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

所以你的SFINAE是错误的:

  • std::result\u of \u t
    缺少
    m\u pFs
    参数:

  • std::测试结果

如果您添加
FSPtr