如何让以下代码clang编译通过?

这是我的代码,gcc编译通过,运行成功,clang编译不通过
原谅我写的这么复杂,项目里有用到这个,现在要用clang编译,这个就不通过了,我的项目有大量的reinterpret_cast<bool (*)(...)>转换不同的方法,所以这块逻辑有没有可替代的方法,让clang编译通过

clang-14
c++17
gcc 9.4

#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool (*ins_func)(...);
bool save(int a,vector<long> arr)
{
        cout << a << endl;
        cout <<   " hello " << endl;
        return true;
}
template <typename T, typename... Args>
bool sum_super_cool(T v, Args... args) {
        cout << "pre" << endl;
        bool ret = (*ins_func)(std::forward<Args>(args)...);
        return ret;
}

int main(int argc, char** argv) {
    ins_func = reinterpret_cast<bool (*)(...)>(&save);
    vector<long> arr;
    arr.push_back(123);
    sum_super_cool(1, 2, arr);

    return 0;
}

image.png

阅读 1.8k
1 个回答

clang 是对的,程序不合法。

expr.call

6 . Calling a function through an expression whose function type is different from the function type of the called function's definition results in undefined behavior.

12 . When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg.
The lvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are performed on the argument expression. An argument that has type cv std::nullptr_t is converted to type void*. After these conversions, if the argument does not have arithmetic, enumeration, pointer, pointer-to-member, or class type, the program is ill-formed.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题