Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there pointer to member traits or something like this?

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.

like image 316
Alexey Malistov Avatar asked Nov 14 '22 13:11

Alexey Malistov


1 Answers

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.

like image 138
3 revs Avatar answered Dec 19 '22 08:12

3 revs