为什么无法推导出嵌套在模板类中的枚举的模板参数?
Why can't the template parameters for enums nested in a template class be deduced?
我有一些常量是这样命名的:
template<int n> class usart {
private:
usart();
public:
enum class tx {};
enum class rx {};
enum class ck {};
};
template<> class usart<1> {
public:
enum class tx { A9 = gpio::A9, C4 = gpio::C4 };
enum class rx { A10 = gpio::A10, C5 = gpio::C5 };
enum class ck { A8 = gpio::A8 };
};
// two more of these
其中gpio
只是一个简单的整数枚举。
我想在另一个文件中对我的类强制实施某种类型安全:
class USART {
public:
template<int N>
USART(typename usart<N>::tx pin_tx, typename usart<N>::rx pin_rx) {
//This signature enforces correct pins with types, doesn't it?
}
};
但是,当我将其与
USART us = USART(usart<1>::tx::A9, usart<1>::rx::A10);
我收到错误
error: expected ')' before 'pin_tx'
为什么此语法是非法的?编辑: typename
现在,当我尝试实例化类时,这给了我这个错误:
error: no matching function for call to 'USART::USART(usart<1>::tx, usart<1>::rx)' note: template<int N> USART::USART(typename usart<N>::tx, typename usart<N>::rx) note: template argument deduction/substitution failed: note: couldn't deduce template parameter 'N'
函数参数中使用的模板参数不可推导,因为这些参数是依赖类型。
"但这很愚蠢!"你会说;"很明显,N是1!为什么编译器不能有智慧来推断这一点呢?
请考虑以下事项:
template<> class usart<4321> {
public:
typedef usart<1>::tx tx;
typedef usart<1>::rx rx;
typedef usart<1>::ck ck;
};
N 应该是 1 还是 4321?毕竟,usart<4321>::tx
和usart<1>::tx
是同一类型。
编译器无法知道 N 应该是什么,除非检查 usart 只有一个实例具有与tx
成员完全相同的类型。这要么需要太多的实例化,要么需要太复杂的逻辑来证明在一般情况下没有实例化会导致这种情况。当然,为这种特殊情况实现一些东西可能很简单,但这对所有其他情况并不是很有用。C++委员会只是决定不要求编译器编写者这样做。
相关文章:
- 有没有办法使用类的私有枚举值作为模板参数?
- 从可变参数创建枚举?
- 在运行时使用枚举器值作为模板函数的模板参数的元程序
- 基于枚举参数调用专用模板方法
- 枚举作为模板参数
- 如何将枚举类作为类模板参数包含在内?
- 如何从枚举类值中指定模板函数参数中的数组大小?
- 如何根据枚举参数返回其他类型
- MSVC编译器错误将模板与枚举参数重载时
- 根据模板枚举参数更改行为
- 如何在 c++ 中使用枚举参数实例化对象
- C :两个功能,每个功能都采用枚举参数,签名匹配
- QT:将强类型枚举参数传递到插槽
- 如何干净地创建类型安全的枚举参数
- 构造构造函数采用单个枚举参数的临时对象时出错
- 为枚举参数化的模板
- 如何使用QT5中的lambdas将具有int参数的信号连接到具有枚举参数with的插槽
- 带有枚举参数的QObject::connect()
- 枚举参数函数和开关优化
- 使用(非类型)枚举参数定义内部类成员函数模板