clang 错误:无法通过可变参数方法传递非平凡类型 'std::vector' 的对象;调用将在运行时中止 [-Wnon-pod-varargs] [重复]

2023-12-07

我用gcc构建,编译成功,运行成功! 但是当我用 clang 构建我的仓库时,我遇到了编译错误!

这是一个错误,其他错误类似

./engine/dispatcher.h:74:57: error: cannot pass object of non-trivial type 'std::vector<long>' through variadic method; call will abort at runtime [-Wnon-pod-varargs]
  bool ret = (this->runner->*ins_func[func_i][dtype_i])(std::forward<Args>(args)...);

许多函数调用此代码

template <class RunnerType>
template <typename... Args>
bool Dispatcher<RunnerType>::dispatcher(const int func_i, const int dtype_i, Args &&...args) {
bool ret = (this->runner->*ins_func[func_i][dtype_i])(std::forward<Args>(args)...);
}

陈述

template <typename RunnerType>
class Dispatcher {
 public:
bool (RunnerType::*ins_func[INSTRUCTION_NUM][DTYPE_NUM])(...);
}

其他相关代码

template <typename RunnerType>
void Dispatcher<RunnerType>::init_instructions_func() {
  ins_func[privpy::func::SAVE][privpy::dtype::INT8] = reinterpret_cast<bool (RunnerType::*)(...)>(
      &RunnerType::template save<int8_t, typename RunnerType::TypeSet::INUMT8>);
  ins_func[privpy::func::SAVE][privpy::dtype::INT16] = reinterpret_cast<bool (RunnerType::*)(...)>(
      &RunnerType::template save<int16_t, typename RunnerType::TypeSet::INUMT16>);
}

clang-版本:14.0.0
操作系统:ubuntu20.04

我写了一个demo来重现这个问题,显示同样的错误

#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;
}
root@3e53105276e1:~/test/main# clang++-14 variable_arg.cpp -std=c++17
variable_arg.cpp:17:25: error: cannot pass object of non-trivial type 'std::vector<long>' through variadic function; call will abort at runtime [-Wnon-pod-varargs]
        bool ret = (*ins_func)(std::forward<Args>(args)...);
                               ^
variable_arg.cpp:25:5: note: in instantiation of function template specialization 'sum_super_cool<int, int, std::vector<long>>' requested here
    sum_super_cool(1, 2, arr);
    ^
1 error generated.

为什么不将该函数作为模板参数传递:

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

bool save(int a, std::vector<long> arr)
{
    std::cout << a << '\n';
    std::cout << " hello \n";
    return true;
}

template <typename T, typename Callable, typename... Args>
bool sum_super_cool(T v, Callable ins_func, Args&&... args)
{
    std::cout << "pre\n";
    return ins_func(std::forward<Args>(args)...);
}

int main(int argc, char** argv) {
    std::vector<long> arr;
    arr.push_back(123);
    sum_super_cool(1, save, 2, arr);

    return 0;
}

This 工作正常没有任何reinterpret_casting 巫毒。这几乎总是一个巨大的危险信号!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

clang 错误:无法通过可变参数方法传递非平凡类型 'std::vector' 的对象;调用将在运行时中止 [-Wnon-pod-varargs] [重复] 的相关文章

  • 在 OnModelCreating 期间设置列名称

    Issue 我目前正在尝试通过设置的属性为我的表及其列添加前缀 我正在使用实体框架核心 我已经正确地为表名添加了前缀 但我似乎无法弄清楚列的前缀 我有一种感觉 我需要使用反射 我已经留下了我的 可能很糟糕的 反思尝试 有人有办法在实体中设置
  • 在现代 C++ 中,临时生命周期延长何时有用?

    在 C 中 您可以将函数的返回值 返回值 而不是引用 绑定到 const 引用 并且代码仍然有效 因为该临时对象的生命周期将延长到作用域末尾 例如 std string get string return abc void f const
  • std::call_once 可重入且线程安全吗?

    std call once http en cppreference com w cpp thread call once是线程安全的 但它也是可重入的吗 我使用 VS2012 调试和发布 进行的测试表明 调用std call once从单
  • 运行需要 MySql.Data 的内置 .NET 应用程序

    我在运行我编写的内置 NET 应用程序时遇到问题 我的应用程序使用最新的 MySql 连接器 该连接器安装在我的系统上 当我尝试将其添加为引用时 该连接器显示为 NET 4 Framwork 组件 当我在环境中以调试模式运行应用程序时 一切
  • Gwan C#,如何获取HTTP标头?

    我需要它来重写 url 以了解我正在处理哪个友好的 url 用于用户代理和其他东西 EDIT public class Gwan MethodImplAttribute MethodImplOptions InternalCall exte
  • 在 omp 并行 for 循环中使用 unique_ptr 会导致 SEG.FAULT

    采取以下代码 include
  • C# 开源 NMEA 解析器 [已关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找 C 开源 NMEA 解析器 嗯 我自己也不熟悉 但是一些快速搜索显示了一个代码项目 htt
  • 将表(行)与 OpenXML SDK 2.5 保持在一起

    我想在 Word 文档中生成多个表 每行 2 行 但我想将这两行保留在一起 如果可能的话 new KeepNext 第一行不起作用 new KeepNext 第一行的最后一段不起作用 new CantSplit 放在桌子上不起作用 在所有情
  • 获取 boost Spirit 语法中的当前行

    我正在尝试使用 boostspirit 获取正在解析的文件的当前行 我创建了一个语法类和结构来解析我的命令 我还想跟踪在哪一行找到命令并将其解析到我的结构中 我将 istream 文件迭代器包装在 multi pass 迭代器中 然后将其包
  • 使用查询表达式对 List 进行排序

    我在使用 Linq 订购这样的结构时遇到问题 public class Person public int ID get set public List
  • C# 编译器不会优化不必要的强制转换

    前几天 在写答案的时候这个问题 https stackoverflow com questions 2208315 why is any slower than contains在这里 关于溢出 我对 C 编译器感到有点惊讶 它没有按照我的
  • 引用/指针失效到底是什么?

    我找不到任何定义指针 引用无效在标准中 我问这个问题是因为我刚刚发现 C 11 禁止字符串的写时复制 COW 据我了解 如果应用了 COW 那么p仍然是一个有效的指针并且r以下命令后的有效参考 std string s abc std st
  • 如何从 Rx Subscribe 回调异步函数?

    我想回调 Rx 订阅中的异步函数 例如 像那样 public class Consumer private readonly Service service new Service public ReplaySubject
  • .NET 4 的条件编译[重复]

    这个问题在这里已经有答案了 可能的重复 条件编译和框架目标 https stackoverflow com questions 2923210 c sharp conditional compilation and framework ta
  • LINQ 中的“from..where”或“FirstOrDefault”

    传统上 当我尝试从数据库中获取用户的数据时 我使用了以下方法 在某种程度上 DbUsers curUser context DbUsers FirstOrDefault x gt x u LoginName id string name c
  • DataContractSerializer 事件/委托字段问题

    在我的 WPF 应用程序中 我正在使用DataContractSerializer序列化对象 我发现它无法序列化具有事件或委托声明的类型 考虑以下失败的代码 Serializable public abstract class BaseCl
  • 如何将 SQL“LIKE”与 LINQ to Entities 结合使用?

    我有一个文本框 允许用户指定搜索字符串 包括通配符 例如 Joh Johnson mit ack on 在使用 LINQ to Entities 之前 我有一个存储过程 该存储过程将该字符串作为参数并执行以下操作 SELECT FROM T
  • 结构化绑定的用例有哪些?

    C 17 标准引入了新的结构化绑定 http en cppreference com w cpp language structured binding功能 最初是proposed http www open std org jtc1 sc
  • 为什么匹配模板类上的部分类模板特化与没有模板匹配的另一个部分特化不明确?

    这个问题可能很难用标题中的句子来描述 但这里有一个最小的例子 include
  • 使用未分配的局部变量

    我遇到了一个错误 尽管声明了变量 failturetext 和 userName 错误仍然出现 谁能帮帮我吗 Use of Unassigned local variable FailureText Use of Unassigned lo

随机推荐