VS 2013 SFINAE 缺陷的解决方法

Workaround for VS 2013 SFINAE deficiencies

本文关键字:解决 方法 缺陷 2013 SFINAE VS      更新时间:2023-10-16

>我正在尝试修复一个目前无法使用VS 2013在Windows上编译的库(实体x)。 它在带有gcc的Linux上编译良好,在带有MinGW的Windows上也可以很好地编译。

似乎问题出在 SFINAE 上 - 我想 VS 2013 没有正确忽略模板的替换失败。

在Microsoft Connect上有关于这个问题的报告,在这里。

在我深入研究实体x之前,有一个问题示例(取自Microsoft Connect的报告):

#include <vector>
#include <future>
using namespace std;
typedef int async_io_op;
struct Foo
{
    //! Invoke the specified callable when the supplied operation completes
    template<class R> inline std::pair < std::vector < future < R >> , std::vector < async_io_op >> call(const std::vector<async_io_op> &ops, const std::vector < std::function < R() >> &callables);
    //! Invoke the specified callable when the supplied operation completes
    template<class R> std::pair < std::vector < future < R >> , std::vector < async_io_op >> call(const std::vector < std::function < R() >> &callables) { return call(std::vector<async_io_op>(), callables); }
    //! Invoke the specified callable when the supplied operation completes
    template<class R> inline std::pair<future<R>, async_io_op> call(const async_io_op &req, std::function<R()> callback);
    //! Invoke the specified callable when the supplied operation completes
    template<class C, class... Args> inline std::pair<future<typename std::result_of<C(Args...)>::type>, async_io_op> call(const async_io_op &req, C callback, Args... args);
};
int main(void)
{
    Foo foo;
    std::vector<async_io_op> ops;
    std::vector < std::function < int() >> callables;
    foo.call(ops, std::move(callables));
    return 0;
}

尝试编译时出现以下错误:

错误 C2064:术语的计算结果不为 0 个参数的函数

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\XrefWrap 58

显然,我们可以使用std::enable_if来解决此问题。 但是,我不知道如何。

有谁知道我如何解决这个编译错误?

编辑:VS 2013的完整输出:

1>------ Build started: Project: VS2013_SFINAE_Failure, Configuration: Debug Win32 ------
1>  test_case.cpp
1>c:program files (x86)microsoft visual studio 12.0vcincludexrefwrap(58): error C2064: term does not evaluate to a function taking 0 arguments
1>          c:program files (x86)microsoft visual studio 12.0vcincludexrefwrap(118) : see reference to class template instantiation 'std::_Result_of<_Fty,>' being compiled
1>          with
1>          [
1>              _Fty=std::vector<std::function<int (void)>,std::allocator<std::function<int (void)>>>
1>          ]
1>          c:usersjarrettdownloadsvs2013_sfinae_failurevs2013_sfinae_failuretest_case.cpp(25) : see reference to class template instantiation 'std::result_of<std::vector<std::function<int (void)>,std::allocator<_Ty>> (void)>' being compiled
1>          with
1>          [
1>              _Ty=std::function<int (void)>
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

>如果将std::result_of替换为其decltype + std::declval等效项,VS2013将编译代码。因此,将最后一个Foo::call()定义更改为

template<class C, class... Args>
inline pair<future<decltype(declval<C>()(declval<Args>()...))>, async_io_op> 
call(const async_io_op& req, C callback, Args... args);

如果我正确理解了错误,它与此处描述的缺陷有关,但令人惊讶的是,GCC 和 clang 都设法编译了result_of代码而没有错误。