c++ - 如何抛出 "template parameter"类型的异常?

标签 c++

我有这样的模板标题:

template<class TypeA, size_t tsize=100, class Exc=std::out_of_range>

和一个抛出 Exc 类型异常的添加函数:

void add(TypeA* objA) {
    if(nelems==capac) {
        delete objA;
        throw Exc e; //the line in question
    }
    nelems++;
    elems[nelems-1]=objA;
}

我有以下错误信息:

error: expected primary-expression before ‘e’
throw Exc e;

我做错了什么?

最佳答案

根据 this page , 表达式 throw 需要另一个表达式。

但您提供的throw 表达式带有声明,这不是表达式

尝试

Exc e{"message"};
throw e;

throw Exc{"message"};

正如贾斯汀对你的问题所建议的那样。

关于c++ - 如何抛出 "template parameter"类型的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50032866/

相关文章:

c++ - 导致神秘暂停的内存分配器

c++ - DirectWrite 文本位置因字体大小而异

c++ - 访问集合中的元素?

c++ - R - ggplot2 使用什么作为其绘图后端?

c++ - 我可以在 void 函数中返回吗?

c++ - 使用 Visual Studio 2019 和 cmake for x86 编译 pe-parse 库

c++ - TCMalloc - 获取指针的分配大小

c++ 使用单行 m.swap(std::stringstream()); 清除字符串流?

c++ - 如何格式化此输出 C++

c++ - 线程作为类,如何保证子类的数据被销毁?