1

I'd like to make a traits class to deduce the class and member types from the pointer to a class member -- a single template parameter.

e.g. given

struct Example { int val; };

, I want a class

pointer_member<&Example::val>

with type definitions class_type=Example, member_type=int and the pointer-to-member itself.

I know c++17 can do this with template <auto>, but I'm stuck with c++11.

I know I could explicitly specify the class and member typenames preceding the pointer-to-member parameter. I hate redundancy. The compiler should know everything from &Example::val.

I know I could create a template function having template parameters <typename class_type,typename member_type> deduced by calling with a function argument member_type class_type::*ptr_to_mem. I'd even be willing to use the decltype() of such a call. Unfortunately, I need to access the specific member ptr_to_mem as a template parameter, not (just) as a passed argument. I cannot see how.

Mark Borgerding
  • 8,117
  • 4
  • 30
  • 51
  • If you are ok using `decltype`, can't you combine that with `std::declval` to get what you want? – super Jul 27 '22 at 21:34
  • 1
    Why do you need `ptr_to_mem` to be a template parameter? – NathanOliver Jul 27 '22 at 21:42
  • `ptr_to_mem` is to be used in a static method, thus it must be in the type, not a variable. – Mark Borgerding Jul 27 '22 at 21:44
  • I don't believe this syntax is possible in C++11. C++17 invented `template ` precisely to make it possible. – Igor Tandetnik Jul 27 '22 at 21:47
  • 1
    Does this answer your question? [Pointer to class member as a template parameter](https://stackoverflow.com/questions/15148749/pointer-to-class-member-as-a-template-parameter) – super Jul 27 '22 at 21:53
  • @super The poster of that question was satisfied with a C++17 solution. – Mark Borgerding Jul 27 '22 at 22:06
  • @MarkBorgerding There is an answer for C++ 11 in link provided by **@super** ! You should really make an effort to read it. And by the way, it is not acceptable to still use C++ 11 in 2022 considering how much C++14 and C++ 17 simplifie a lot of things. – Phil1970 Jul 28 '22 at 00:00
  • 2
    "Not acceptable to still use C++ 11"? If you don't want to help, fine, but don't tell people their problems are not worth solving. We don't all force our customers to comply. The accepted answer of the other question was only for C++17 and later. Once an answer is accepted, people tend to stop trying. Yes, I saw someone suggest using the preprocessor to duplicate the argument. That's arguably not C++, though it might be the least smelly option. – Mark Borgerding Jul 28 '22 at 03:22
  • @MarkBorgerding There is the solution, which is covered by the accepted answer. Then there is the only viable alternative to pre-c++17 and the fact that there is no better solution. Not sure what more you want? – super Jul 28 '22 at 07:31
  • Assertions of impossibility are not proof. I understand that the preprocessor _might_ be the best way for C++11. Forgive me if I entertain the possibility it is not. – Mark Borgerding Jul 28 '22 at 13:01
  • @MarkBorgerding The question has been asked at least 9 years ago. If there is no solution at this point, I doubt there will be one. Not to mention the fact that this was one of the very reasons the `auto` specifier in template was introduced in c++17. If you look up the proposals I'm fairly sure you will find an example very close to this one as one of the motivators for the needs of it's inclusion. – super Jul 28 '22 at 20:04

2 Answers2

0

The Motivation and Scope of P0127r1 suggest this is not possible with C++11.

ManuelAtWorks' answer appears the best option for C++11.

Mark Borgerding
  • 8,117
  • 4
  • 30
  • 51
0

In c++17, template <auto value> which allows pointer_member<&Example::val> usage.

For previous version, there are workaround, such as template <typename T, T value> with usage changed to pointer_member<decltype(&Example::val), &Example::val> MACRO can simplify usage a little:

#define AUTO(value) decltype(value), value

and then

pointer_member<AUTO(&Example::val)>
Jarod42
  • 203,559
  • 14
  • 181
  • 302