小编mew*_*234的帖子

我可以将函数指针void*转换为std :: function吗?

函数指针void*加载dlsym(),我可以把它投射到std::function

假设lib中的函数声明是 int func(int);

using FuncType = std::function<int(int)>;
FuncType f = dlsym(libHandle, "func"); // can this work?
FuncType f = reinterpret_cast<FuncType>(dlsym(libHandle, "func")); // how about this?
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++14

7
推荐指数
1
解决办法
956
查看次数

我可以用constexpr函数声明一个静态数组

// since we can dedfine a static array like this
int a[5] = {0};
// decltype(a[5]) is `int [5]`

// Then how about this way?
constexpr int sum(int a, int b) { return a + b; }
int a, b;
std::cin >> a >> b;
int arr[sum(a, b)] = {0};
Run Code Online (Sandbox Code Playgroud)

它可以成功编译,但是是arr一个静态数组?当我尝试arrtypeid().name()或打印类型时boost::typeindex::type_id_with_cvr,我得到以下错误:

error: cannot create type information for type 'int [(<anonymous> + 1)]' because it involves types of variable size
 std::cout << typeid(decltype(arr)).name() …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11

2
推荐指数
1
解决办法
146
查看次数

标签 统计

c++ ×2

c++11 ×2

c++14 ×1

constexpr ×1