c++ - MSVC : unexpected token '__cdecl' , 预期 'expression'

标签 c++ visual-studio-2017

将应用程序移植到 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/

相关文章:

c# - 升级到 VS2017 后找不到任何适合该文化或中立文化错误的资源

c++ - 在 Visual Studio C++ 上使用 CUDA 编译错误 CMAKE

c++ - C++11 中的原始指针与智能指针

c++ - SIGSEGV(段错误)C++ 指针

c++ - 与安全 bool 习语相关的运算符重载和隐式转换为 bool

c++ - 从不传递 long long 作为参数?

c++ - 如何为 WinApi ldap_search_s 初始化 PZPWSTR (wchar_t**) 参数?

c++ - CMake 无法找到 Boost 库(VS 2017)

c# - HTTP 错误 500.19,错误代码为 0x8007000d visual studio 2017,同时部署 .net 核心应用程序

visual-studio - Visual Studio 文件夹 View 上下文菜单