没有typedef的返回函数指针错误?

时间:2017-02-10 03:58:22

标签: c++ function function-pointers

我试图写一个函数,它可以返回" malloc"函数指针。

但是有一个错误,该平台是Windows 7 + Visual Studio 2015。

源代码" malloc.cpp":

#include <cstdlib>


namespace portable {

    static void *(*malloc_impl)(size_t) = std::malloc;
    static void (*free_impl)(void*) = std::free;
    static void *(*realloc_impl)(void*, size_t) = std::realloc;
    static void *(*calloc_impl)(size_t, size_t) = std::calloc;

#if defined(_WIN32) && defined(__MINGW32__)
#   error("sorry, we don't support MinGW32 compiler with Windows")
#endif

#if defined(_WIN32) && defined(_MSC_VER)
#   include <WinSock2.h>
#   include <Windows.h>
    static size_t (*malloc_usable_size_impl)(void*) = _msize;
#elif defined(__APPLE__)
#   include <malloc.h>
    static size_t (*malloc_usable_size_impl)(void*) = malloc_size;
#else
#   include <malloc.h>
    static size_t (*malloc_usable_size_impl)(void*) = malloc_usable_size;
#endif

    void *( *o_set_malloc( void *(*func)(size_t) ) ) (size_t) throw()
    {
        void *(*old)(size_t) = malloc_impl;
        if (func) {
            malloc_impl = func;
        }
        return old;
    }
    size_t ( *o_set_malloc_usable_size( size_t (*func)(void*) ) ) (void*) throw()
    {
        size_t (*old)(void*) = malloc_usable_size_impl;
        if (func) {
            malloc_usable_size_impl = func;
        }
        return old;
    }
    void ( *o_set_free( void (*func)(void*) ) ) (void*) throw()
    {
        void (*old)(void*) = free_impl;
        if (func) {
            free_impl = func;
        }
        return old;
    }
    void *( *o_set_realloc( void *(*func)(void*, size_t) ) ) (void*, size_t) throw()
    {
        void *(*old)(void*, size_t) = realloc_impl;
        if (func) {
            realloc_impl = func;
        }
        return old;
    }
    void *( *o_set_calloc( void *(*func)(size_t, size_t) ) ) (size_t, size_t) throw()
    {
        void *(*old)(size_t, size_t) = calloc_impl;
        if (func) {
            calloc_impl = func;
        }
        return old;
    }
}

编译器报告错误:

严重级代码描述项目文件行抑制状态 错误(活动)

不兼容的异常规范

d:\ work \ project \ orange \ src \ portable \ malloc.cpp 56

似乎&#34; old&#34;与返回类型不兼容。请帮帮我。

1 个答案:

答案 0 :(得分:-1)

感谢评论,我发现了问题。

在Windows 7 + VS2015上,我定义了类似的内容:

#define NOEXCEPT throw()

不兼容的是“void *(*)(size_t)”和“void *(*)(size_t) throw()”。