朋友模板类的依赖类型名称

时间:2016-08-17 16:59:27

标签: c++ templates c++11 friend-function dependent-name

我有3个课程,DeviceRegisterWriteOnlyPolicy,定义如下:

设备

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);
    }
}

WriteOnlyPolicy

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);
    }
};

RegisterWriteOnlyPolicy实际上从未在此方案中实例化,但我希望该策略调用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令牌

0 个答案:

没有答案