I want to use a pointer to a class member as a template parameter as in:
template <class Class, class Result, Result Class::*Member>
struct MyStruct {
// ...
};
Using this struct like MyStruct<SomeClass, SomeResult, &SomeClass::value> variable
works just fine, but I don't like that I have to specify SomeClass
and SomeResult
.
I would like to use MyStruct<&SomeClass::value> variable
if that is possible, but without losing the ability to pass any class and have any result type.
I tried the following, but the syntax is illegal:
template <class Class, class Result>
template <Result Class::*Member>
struct MyStruct {
// ...
};
error: too many template-parameter-lists
I tried using a helper function (that does actually work in Clang but is refused by GCC):
template <class Class, class Result>
static constexpr auto makeMyStruct(Result Class::*member) ->
MyStruct<Class, Result, member> {
// ...
}
error: use of parameter `member' outside function body
error: template argument 3 is invalid
Is it possible to have a simple MyStruct<&SomeClass::value>
, and if so, how?
Related question that did not solve my question:
member
as a template argument, which must be known at compile-time to instantiate the template. If you were to change MyStruct to take theResult Class::*member
as a constructor parameter instead of template parameter then it works fine. – Chasechaserauto x = makeMyStruct(&SomeStruct::some_member);
. N3601 comes in handy in this case. – Golightly