指向类成员的模板参数 [英] Pointer to class member as template parameter

查看:183
本文介绍了指向类成员的模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能有非类型模板参数,实际上是一个类成员的指针?我想要做的是类似下面的:

Is it possible to have non-type template parameter which is actually a pointer to a class member? What I'm looking to do is something like the following:

struct Person {
  Dog dog;
};

template <?? ptr>
struct Strange {
  // ...
};

typedef Strange<&Person::dog> weird;

到目前为止,我的工作使我相信没有什么是可能的,

My work so far leads me to believe that nothing of the sort is possible, but I'm curious if anyone has can say otherwise.

推荐答案

非类型模板参数必须具有以下类型之一(可选cv-qualaled):

A non-type template-parameter shall have one of the following (optionally cv-qualified) types:


  • 整数或枚举类型,

  • 指向对象或函数指针的指针,

  • 引用对象或函数引用,

  • 指向会员的指针。

  • integral or enumeration type,
  • pointer to object or pointer to function,
  • reference to object or reference to function,
  • pointer to member.

允许,似乎工作在 g ++ 像这样:

So it is allowed, and seems to work on g++ like this:

template <Dog Person::*ptr>
struct Strange { ... };

这篇关于指向类成员的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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