我有 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;
会做的伎俩,但我得到了错误
错误:';'之前的预期不合格ID 令牌