具有编译时间常数的模板专业化 [英] Template specialization with compile time constant

查看:84
本文介绍了具有编译时间常数的模板专业化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为具有编译时间常数的模板类建立专门化的版本.

I am trying to build a specialization for a template class with a compile time constant.

模板类如下:

template<class TNativeItem, class TComItem = void,
         VARTYPE _vartype = _ATL_AutomationType<TComItem>::type>
class InOutComArray
{
private:
    CComSafeArray<TComItem, _vartype> _safeArray;
    // ...
public:
    InOutComArray(
        TNativeItem* items, size_t length,
        std::function<TComItem(const TNativeItem&)> convertToCom,
        std::function<TNativeItem(const TComItem&)> convertFromCom)
        : _safeArray(length)
    {
        // ...
    }

    // ...
};

用法例如:

InOutComArray<BOOL, VARIANT_BOOL, VT_BOOL>(
    items, length, BOOLToVARIANT_BOOL, VARIANT_BOOLToBOOL));

但是,也有一些类型不需要转换,我想为此提供一个简短的版本:

However, there also exist types that don't require conversion and I wanted to provide a short hand version for this:

InOutComArray<LONG>(items, length);

我试图这样实现:

template<class TItem, VARTYPE _vartype = _ATL_AutomationType<TItem>::type>
class InOutComArray<TItem, void, _vartype>
    : public InOutComArray<TItem, TItem, _vartype>
{
public:
    InOutComArray(TItem* items, size_t length)
        : InOutComArray<TItem, TItem, _vartype>(
              items, length, NoConverter<TItem>, NoConverter<TItem>)
    {

    }
};

但是,出现以下错误:

'_ vartype':部分专业化不允许使用默认模板参数

'_vartype' : default template arguments not allowed on a partial specialization

有什么办法解决吗?

推荐答案

您首先将默认参数定义为 void _ATL_AutomationType< TComItem> :: type ,因此当只给出一个参数X时,您希望 InOutComArray< X> 成为 InOutComArray< X,void,_ATL_AutomationType< void> :: type> .

You first define the default arguments to be void and _ATL_AutomationType<TComItem>::type, so when exactly one argument X is given, you want InOutComArray<X> to be an InOutComArray<X, void, _ATL_AutomationType<void>::type>.

您的部分专业化与此矛盾: InOutComArray< X> 应该是 InOutComArray< X,X,_ATL_AutomationType< X> :: type> .

Your partial specialization contradicts this: InOutComArray<X> shall be a InOutComArray<X, X, _ATL_AutomationType<X>::type>.

根据您认为更可能是第二个参数(即 void 或与第一个参数相同)的不同,可以将第二个参数默认设置为第一个参数:

Depending on what you thik will be more likely second argument, (i.e. void or the same as the first argument), you could make the second argument defaulted to the first one in the first place:

template<class TNativeItem, class TComItem = TNativeItem,
     VARTYPE _vartype = _ATL_AutomationType<TComItem>::type>

这样,除了额外的构造函数之外,还涵盖了部分专业化的行为.这可以通过使用构造函数的默认参数来实现:

That way the behavior of the partial specialization is covered, except for the additional constructor. This can be achieved by using default arguments for the constructor:

template<class TNativeItem, class TComItem = TNativeItem,
     VARTYPE _vartype = _ATL_AutomationType<TComItem>::type>
class InOutComArray
{
public:
InOutComArray(
    TNativeItem* items, size_t length,
    std::function<TComItem(const TNativeItem&)> convertToCom = NoConverter<TNativeItem>(),
    std::function<TNativeItem(const TComItem&)> convertFromCom = NoConverter<TNativeItem>());
};

这篇关于具有编译时间常数的模板专业化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆