有指向成员traits或类似这样的指针吗? [英] Is there pointer to member traits or something like this?

查看:90
本文介绍了有指向成员traits或类似这样的指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据其他我的问题。 / p>

请考虑以下代码

  template< typename T,int N& 
struct A {
typedef T value_type; // save T to value_type
static const int size = N; // save N to size
};看看,我可以使用 value_type


$ b size 作为模板参数。

  typedef A< int,2> A1; 
typedef A< A1 :: value_type,A1 :: size + 3> A2; // OK,A2是A

现在我想对指向成员的指针做同样的事情:

  struct Foo {
int m;
int r;
};

template< int Foo :: * Mem>
struct B {
static int Foo :: * const mp;
};

template< int Foo :: * Mem>
int Foo :: * const B< Mem> :: mp = Mem; //保存指向成员的指针

但我收到错误。

  typedef B<& Foo :: m> B1; 
typedef B< B1 :: mp> B2; //不工作

如何使最后一行工作?
或如何获取简单结果?



注意。我知道它不工作。不需要指向C ++标准的链接。
我需要解决方法。

解决方案

它不应该根据C ++标准5.19 / 2:


其他表达式被视为常量表达式仅用于非本地静态对象初始化(3.6.2)。这种常量表达式应该计算为以下之一:

- 空指针值(4.10),

- 空成员指针值(4.11),

- 算术常数表达式,

- 地址常量表达式,

- 引用常量表达式,

- 完整对象类型的地址常量表达式,加上或减去整数常数表达式



- 指向成员常量表达式的指针




这不是原始问题的答案,但它是错误的语句。


Based on other my question.

Consider the following code

template<typename T, int N>
struct A {
  typedef T value_type; // save T to value_type
  static const int size = N; // save N to size
};

Look, I can use value_type and size as template parameter.

typedef A<int, 2> A1;
typedef A<A1::value_type, A1::size + 3> A2;  // OK,  A2 is A<int,5>

Now I want to do the same with pointer to member:

struct Foo {
    int m;
    int r;
};

template<int Foo::*Mem>
struct B {
   static int Foo::* const mp;
};

template<int Foo::*Mem>
int Foo::* const B<Mem>::mp = Mem; // Save pointer to member

But I get error.

typedef B<&Foo::m> B1;
typedef B<B1::mp>  B2;  // DOES NOT WORK

How to make last line to work? Or how to get similiary result?

Note. I know that it does not work. No links to C++ Standard is needed. I need workaround.

解决方案

It shouldn't work according to C++ Standard 5.19/2:

Other expressions are considered constant-expressions only for the purpose of non-local static object initialization (3.6.2). Such constant expressions shall evaluate to one of the following:
— a null pointer value (4.10),
— a null member pointer value (4.11),
— an arithmetic constant expression,
— an address constant expression,
— a reference constant expression,
— an address constant expression for a complete object type, plus or minus an integral constant expression,
or
a pointer to member constant expression.

It is not the answer to the original question, but it is the answer to this wrong statement.

这篇关于有指向成员traits或类似这样的指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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