专门用于GUID值的模板 [英] Specialize a template for a GUID value

查看:223
本文介绍了专门用于GUID值的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为什么不是字段的后续操作从常量POD对象常量本身?



库中的头声明类GUID如

  static const GUID CLSID_EH264VD = 
{0x96b9d0ed,0x8d13,0x4171,{0xa9,0x83,0xb8,0x4d,0x88,0xd6,0x27,0xbe}};

我想写一个直接从dll创建对象的函数,而不需要dll注册,所以我需要将每个CLSID映射到dll名称。

 创建< CLSID_EH264VD>()

这将取决于诸如

 的模板< 
struct dll< CLSID_EH264VD>
{
char const * filename =mc_dec_avc_ds.ax;
}

因此,尝试用未知的dll实例化未注册的类是一个编译时错误。



问题是模板不能专门用于GUID。链接的问题说,constexpr允许以一种允许专门化的方式声明GUID,但Visual C ++不支持最新版本(2012)中的constexpr任何解决方法?


p>非类型非模板模板参数的模板参数 之一:



类型为积分或枚举类型的模板参数,模板参数类型的转换常量表达式
(5.19);或



非类型模板参数;或



- 指定具有静态存储持续时间的对象的地址的常量表达式(5.19)和
[...]









b

这意味着即使 GUID 本身不能用作模板参数,也可以使用< GUID 作为参数的 地址以及指向 GUID -type参数:

  template< GUID const * pGuid> 
struct dll {};

模板<>
struct dll<& CLSID_EH264VD>
// ^^^^^^^^^^^^^^
//这是一个常量表达式
{
char const * filename =mc_dec_avc_ds.ax ;
}

// ...

dll<& CLSID_EH264VD> x;


This is a follow up to Why aren't fields from constant POD object constants themselves?

A header from a library declares class GUIDs like

static const GUID CLSID_EH264VD = 
{ 0x96b9d0ed, 0x8d13, 0x4171, { 0xa9, 0x83, 0xb8, 0x4d, 0x88, 0xd6, 0x27, 0xbe } };

I want to write a function that creates an object directly from the dll, without requiring the dll to be registered, so I need to map each CLSID to the dll name. Something like

Create<CLSID_EH264VD>()

which would depend on a specialization such as

template<>
struct dll<CLSID_EH264VD>
{
    char const* filename = ""mc_dec_avc_ds.ax";
}

so that it's a compile time error to try to instantiate an unregistered class with an unknown dll.

The problem is that templates can't be specialized for GUIDs. The linked question says that constexpr would allow to declare the GUID in a way that allows specialization, but Visual C++ doesn't support constexpr in the latest version (2012). Any workaround?

解决方案

Per Paragraph 14.3.2/1 of the C++11 Standard:

A template-argument for a non-type, non-template template-parameter shall be one of:

— for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or

— the name of a non-type template-parameter; or

— a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage, [...]

— [...]

This means that even though the GUID itself cannot be used as a template argument, you can use the address of the global GUID as an argument, and a pointer to GUID as the corresponding non-type parameter:

template<GUID const* pGuid>
struct dll { };

template<>
struct dll<&CLSID_EH264VD>
//       ^^^^^^^^^^^^^^
//       This is a constant expression
{
    char const* filename = ""mc_dec_avc_ds.ax";
}

// ...

dll<&CLSID_EH264VD> x;

这篇关于专门用于GUID值的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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