c++可变模板类终止
C++ variadic template class termination
半小时前我发现了可变模板参数,现在我完全被迷住了。
我有一个基于静态类的抽象微控制器输出引脚。我想组一些输出引脚,所以我可以处理它们作为一个引脚。下面的代码可以工作,但我认为我应该能够在0个参数而不是1个参数上结束递归。
template< typename pin, typename... tail_args >
class tee {
public:
typedef tee< tail_args... > tail;
static void set( bool b ){
pin::set( b );
tail::set( b );
}
};
template< typename pin >
class tee< pin > {
public:
static void set( bool b ){
pin::set( b );
}
};
我尝试了这个,但编译器(gcc)似乎没有考虑到它:
template<>
class tee<> : public pin_output {
public:
static void set( bool b ){}
};
错误消息很长,但它实际上表示没有牙齿<>。是我的牙齿有问题吗<>还是不可能结束递归
一般情况下至少需要1
个参数(pin
),因此您不能创建具有0
个参数的专门化。
相反,您应该使用最通用的情况,接受任何数量的参数:
template< typename... > class tee;
创建专门化:
template< typename pin, typename... tail_args >
class tee<pin, tail_args...> {
public:
typedef tee< tail_args... > tail;
static void set( bool b ){
pin::set( b );
tail::set( b );
}
};
template<>
class tee<> {
public:
static void set( bool b ){}
};
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- llvm构建器向基本块添加终止符
- 如何使用隔离>终止执行来停止所有线程
- 用于将C++代码转换为 Web 程序集的脚本未终止
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 终止 QProcess 不会终止子进程
- 运行代码时,c++ 会终止进程
- 检测到堆栈粉碎:已终止 中止(核心已转储)
- 在输入句子时终止 std::out_of_range
- 二进制搜索的终止点
- MPI:检查是否有任何进程已终止
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 如何改进我的代码,使其不会因超时而终止?(黑客排名挑战)
- while 循环不以哨兵值终止
- C++ 程序不会因为内存而终止
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常