我有3个课程,Device
,Register
和WriteOnlyPolicy
,定义如下:
class Device
{
public:
template<class Register>
inline void write(typename Register::value_type value)
{
Register::writeRegister(value, this);
}
protected:
// limit device-specific API calls to this function
virtual void writeDeviceRegister(uint64_t address, uint64_t value) = 0;
};
template<uint64_t ADDRESS, class ValueType, template<class> class AccessPolicy>
struct Register : public AccessPolicy<ValueType>
{
using value_type = ValueType;
using access_policy = AccessPolicy<ValueType>;
static void writeRegister(value_type value, Device* device)
{
access_policy::write(ADDRESS, device, value);
}
}
template<typename value_type>
class WriteOnlyPolicy
{
protected:
static void write(uint64_t ADDRESS, Device* device, value_type value)
{
// convert from a value_type to a uint64_t...
// Shift/mask as needed...
device->writeDeviceRegister(ADDRESS, value);
}
};
Register
和WriteOnlyPolicy
实际上从未在此方案中实例化,但我希望该策略调用writeDeviceRegister
类的受保护Device
函数。我不希望该函数公开,因为我只想将write
函数暴露给类的用户,而不是特定于设备的实现。
为了允许我希望Device
与朋友Register::access_policy
(在这种情况下是WriteOnlyPolicy
类作为模板参数),但我不能找出朋友的模板类的依赖类型名称的语法。这可能吗?
我认为在friend
类中添加Device
声明,例如
template<uint64_t a, class b, template<class> class c>
friend typename Register<a,b,c>::access_policy;
会做到这一点,但我得到了错误
错误:在&#39;;&#39;之前预期的不合格ID令牌