将应用程序移植到 Windows,我现在尝试使用 VS2017 编译它,但遇到了很多问题。其中之一是我编写的用于使 C++ 成员函数可从 C 库 (FUSE) 调用的模板包装器不起作用:
template <class T> class Callback {};
template <class T, class ...Arguments>
struct Callback<T(Arguments...)>
{
template <T(operations::*CALLBACK)(Arguments...)>
static T wrap(Arguments... parameters)
{
auto *instance = static_cast<operations*>(fuse_get_context()->private_data);
return (instance->*CALLBACK)(std::forward<Arguments>(parameters)...);
}
};
我正在尝试在构造函数中像这样设置回调:
_operations.get_attr = Callback<std::remove_reference<decltype(*_high_level.getattr)>::type>::wrap<&operations::getattr>;
这是 - 我相信 - 有效的代码,但它不会与一些警告和错误一起编译:
warning C4229: anachronism used: modifiers on data are ignored
error C2760: syntax error: unexpected token '__cdecl', expected 'expression'
note: see reference to function template instantiation 'T Callback<int (const char *,stat64_cygwin *)>::wrap<int operations::getattr(const char *,stat64_cygwin *)>(const char *,stat64_cygwin *)' being compiled
with
[
T=int
]
note: see reference to function template instantiation 'T Callback<int (const char *,stat64_cygwin *)>::wrap<int operations::getattr(const char *,stat64_cygwin *)>(const char *,stat64_cygwin *)' being compiled
with
[
T=int
]
error C2059: syntax error: '__cdecl'
有关不合时宜的警告指向包含包装函数模板规范的行。错误指向回调实际被调用和返回的行,在 wrap 函数内。
这很令人困惑,在稍微阅读之后我发现过时的是 Windows API 中使用的那种属性,我在这里不使用它,而且我这里也没有任何 __cdecl。我不知道如何在这里进行。
最佳答案
将 CALLBACK 重命名为 MEMFUNC 有效。显然,Windows 将 CALLBACK 定义为一些意想不到的东西,导致代码以无法编译的方式扩展。
除了像这样定义随机的东西(不在其前面加上 WINDOWS_ 前缀)很奇怪之外,不幸的是,编译器生成的错误不能正确地表明它源自某些 #define,这使得调试变得困难。
关于c++ - MSVC : unexpected token '__cdecl' , 预期 'expression',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187424/