我有一些这样命名的常量:
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
wheregpio
只是一个简单的整数枚举。
我想在另一个文件中对我的类强制执行一些类型安全:
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'