VS 2013 SFINAE 缺陷的解决方法
Workaround for VS 2013 SFINAE deficiencies
>我正在尝试修复一个目前无法使用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
代码而没有错误。
相关文章:
- 有没有办法在 c++ 中同时生成随机数?如果没有,是否有解决方法?
- 从 int 中剥离位时,编译器会警告一个转换,但不警告其他转换.有解决方法吗?
- 是否有解决方法可以在 c++ 中为 short 定义用户定义的文字?
- 不为 emplace() 定义构造函数的解决方法
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 函数模板部分专业化-有什么解决方法吗
- 带boost的过载模糊性:可选,解决方法
- 继承构造函数和其他变量的解决方法
- C++ 解决方法:"从类型"B*"的表达式初始化类型"C*&"的引用无效"
- 对前向声明类型进行unique_ptr的解决方法
- 解决方法:'can not be used when making a shared object; recompile with -fPIC'使用Cmake。使用普通的 g++ 工作
- 解决方法:QPixmap:在GUI线程之外使用pixmap是不安全的
- CreateProcessWithTokenW 的 cmdline 参数的最大长度是否有解决方法?
- C++双精度作为模板参数的解决方法
- 在参数相关查找之前进行模板替换的任何方法(或解决方法?
- CUDA 内核"Only a single pack parameter is allowed"解决方法?
- dlsym() 解决方法返回类型
- VS2017是否有VLA(可变长度阵列)支持解决方法
- 解决方法:未定义对"class::function"的引用
- 来自 Boost 的 JSON 解析器的警告 C4512 的解决方法