小编tuc*_*cio的帖子

编译时间模板实例化检查

是否可以检查模板类型是否已在编译时实例化,以便我可以在enable_if特化中使用此信息?

让我说我有

template <typename T> struct known_type { };
Run Code Online (Sandbox Code Playgroud)

如果在编译时实例化known_type,我可以以某种方式定义一些is_known_type,其值为true吗?

c++ templates type-traits c++11

10
推荐指数
1
解决办法
2942
查看次数

在没有 LD_PRELOAD 的情况下覆盖 libc 函数

我正计划实现自己的 malloc/free,但在尝试将共享库与可执行文件链接时遇到了一些问题。

现在,我可以让它与 LD_PRELOAD 一起工作,但不能通过将 .so 链接到可执行文件,尽管我可以获得类似的库(如 tcmalloc),只需将它们链接到我的可执行文件即可正常工作,并且想要执行以下操作相同的。

我正在使用 cmake 构建所有内容,这是我的共享库的 CMakeLists:

cmake_minimum_required(VERSION 2.8)
project(allocator)

add_library(allocator SHARED exports.cpp)
target_link_libraries(allocator dl)

target_compile_features(allocator PRIVATE cxx_range_for)
Run Code Online (Sandbox Code Playgroud)

这是exports.cpp:

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <dlfcn.h>

typedef void * (*MallocType)(size_t);
typedef void (*FreeType)(void *);

static bool g_initialized = false;

static MallocType real_malloc = nullptr;
static FreeType   real_free   = nullptr;

static void alloc_init(void)
{
    real_malloc = (MallocType) dlsym(RTLD_NEXT, "malloc");

    if (!real_malloc)
    {
        fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
    }

    real_free = (FreeType) dlsym(RTLD_NEXT, "free"); …
Run Code Online (Sandbox Code Playgroud)

c++ linux cmake dynamic-linking

5
推荐指数
1
解决办法
1368
查看次数

标签 统计

c++ ×2

c++11 ×1

cmake ×1

dynamic-linking ×1

linux ×1

templates ×1

type-traits ×1