Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to class member as a template parameter

Tags:

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:

like image 705
kay Avatar asked Mar 01 '13 01:03

kay


People also ask

What do you mean by pointer to class member?

Pointers to members allow you to refer to nonstatic members of class objects. You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object. To point to a static class member, you must use a normal pointer.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.

What is correct for template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Can we pass Nontype parameters to templates?

Template non-type arguments in C++ It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.


2 Answers

In c++17, with the addition of auto in template arguments (P0127), I think you can now do:

template<auto value> struct MyStruct {};  template<typename Class, typename Result, Result Class::* value> struct MyStruct<value> {     // add members using Class, Result, and value here     using containing_type = Class; };  typename MyStruct<&Something::theotherthing>::containing_type x = Something(); 
like image 65
Eric Avatar answered Nov 26 '22 09:11

Eric


An answer to my question was proposed in this paper for the next upcoming C++ standard:

  • https://isocpp.org/files/papers/n3601.html

This syntax was proposed:

template<using typename T, T t> struct some_struct { /* ... */ };  some_struct<&A::f> x; 

The need for a new syntactical construct indicates that you cannot do that by now.

I hope n3601 will be accepted. :-)

like image 44
kay Avatar answered Nov 26 '22 07:11

kay