Topic: Pointer to member template. Is this legal?


Author: german diago <germandiago@gmail.com>
Date: Mon, 1 Mar 2010 12:05:06 CST
Raw View
template <class klass, class PointerToMemFunc, class
PointerToMemFunc2, PointerToMemFunc, PointerToMemFunc2>
class MyClass {
 klass * object_;
};

This works and compiles. Now I would like to know if it's possible to
do something in the lines of (which doesn't work as of now for me):

template ...
class MyClass {
    klass * object_;

    typedef decltype((object_->*PointerToMemberFunc)()) ReturnType;
};

As you can see, there are two parameters that are called the same way.
One of them is a pointer to member function type, the other is an
address to one of those types. I don't know how to disambiguate this.
Can anyone help, please? Thanks in advance.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Tue, 2 Mar 2010 12:12:34 CST
Raw View
On 1 Mrz., 19:05, german diago <germandi...@gmail.com> wrote:
> template <class klass, class PointerToMemFunc, class
> PointerToMemFunc2, PointerToMemFunc, PointerToMemFunc2>
> class MyClass {
>  klass * object_;
> };

Remark: It would have been really helpful, if you would
have reduced the template parameter set to the minimum
set necessary. For the sake of the discussion here I
simplified it to:

template <class klass, class PointerToMemFunc, PointerToMemFunc PM>
class MyClass {
  klass * object_;
};

> This works and compiles. Now I would like to know if it's possible to
> do something in the lines of (which doesn't work as of now for me):
>
> template ...
> class MyClass {
>     klass * object_;
>
>     typedef decltype((object_->*PointerToMemberFunc)()) ReturnType;
> };
>
> As you can see, there are two parameters that are called the same way.
> One of them is a pointer to member function type, the other is an
> address to one of those types. I don't know how to disambiguate this.
> Can anyone help, please? Thanks in advance.

In the above declaration you overcharged the reader with
unnecessary details, but here the relevant information is
missing. I assume you meant:

template <class klass, class PointerToMemFunc, PointerToMemFunc PM>
class MyClass {
  klass * object_;
   typedef decltype((object_->*PM)()) ReturnType;
};

I do not understand your comment regarding disambiguation,
there is no problem with PM at all. The problem is
related to the fact that you cannot refer to the member
object_ in the decltype type specifier. This is quite clear,
because you are not in the body of member function
here and outside of that scope is not defined (You
refer to this indirectly via object_ which is equivalent to
this->object_). This problem is easy to fix:

template<typename T>
T declval(); // Just declared

template <class klass, class PointerToMemFunc,
   PointerToMemFunc PM>
class MyClass {
  klass* object_;
  typedef decltype((declval<klass*>()->*PM)()) ReturnType;
};

HTH & Greetings from Bremen,

Daniel Kr   gler

P.S.: Something like declval will probably be part of
the standard library, see

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#1255



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Tue, 2 Mar 2010 12:35:07 CST
Raw View
german diago wrote:

> template <class klass, class PointerToMemFunc, class
> PointerToMemFunc2, PointerToMemFunc, PointerToMemFunc2>
> class MyClass {
>  klass * object_;
> };
>
> This works and compiles. Now I would like to know if it's possible to
> do something in the lines of (which doesn't work as of now for me):
>
> template ...
> class MyClass {
>     klass * object_;
>
>     typedef decltype((object_->*PointerToMemberFunc)()) ReturnType;
> };
>
> As you can see, there are two parameters that are called the same way.
> One of them is a pointer to member function type, the other is an
> address to one of those types. I don't know how to disambiguate this.
> Can anyone help, please? Thanks in advance.
>

You have to give the pointer a name. Currently you have two unnamed non-type
parameters. Change to

template <class klass, class PointerToMemFunc, class
PointerToMemFunc2, PointerToMemFunc M1, PointerToMemFunc2 M2>
class MyClass {
  klass * object_;
  typedef decltype((object_->*M1)()) ReturnType;
};


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Tue, 2 Mar 2010 12:35:07 CST
Raw View
On Mar 1, 7:05 pm, german diago <germandi...@gmail.com> wrote:
> template <class klass, class PointerToMemFunc, class
> PointerToMemFunc2, PointerToMemFunc, PointerToMemFunc2>
> class MyClass {
>  klass * object_;
>
> };
>
> This works and compiles.

Yes, but do you realize what it does? Note that the last two
parameters of the template, the non-type parameters, are unnamed.

  Now I would like to know if it's possible to
> do something in the lines of (which doesn't work as of now for me):
>
> template ...
> class MyClass {
>     klass * object_;
>
>     typedef decltype((object_->*PointerToMemberFunc)()) ReturnType;

Here's you're doing the syntactic equivalent of (object_->*int)().
Can't work. Give the fourth and fifth parameters names and use those
names instead of the names of the second and third parameter, which
are types.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Thu, 4 Mar 2010 11:05:43 CST
Raw View
Daniel Kr  gler wrote:

> On 1 Mrz., 19:05, german diago <germandi...@gmail.com> wrote:
>> This works and compiles. Now I would like to know if it's possible to
>> do something in the lines of (which doesn't work as of now for me):
>>
>> template ...
>> class MyClass {
>>     klass * object_;
>>
>>     typedef decltype((object_->*PointerToMemberFunc)()) ReturnType;
>> };
>>
>> As you can see, there are two parameters that are called the same way.
>> One of them is a pointer to member function type, the other is an
>> address to one of those types. I don't know how to disambiguate this.
>> Can anyone help, please? Thanks in advance.
>
[snipped]
>
> I do not understand your comment regarding disambiguation,
> there is no problem with PM at all. The problem is
> related to the fact that you cannot refer to the member
> object_ in the decltype type specifier. This is quite clear,
> because you are not in the body of member function
> here and outside of that scope is not defined (You
> refer to this indirectly via object_ which is equivalent to
> this->object_). This problem is easy to fix:
>

I was in the impression that C++0x fixes this. In fact, GCC trunk already
accepts code like

struct A { int a; decltype(a + 1.0) b; };

According to 5.1.1/10 (last bullet) in n3035. "this" is not added if not in
the body of a non-static member function.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Fri, 5 Mar 2010 12:04:45 CST
Raw View
On 4 Mrz., 18:05, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> Daniel Kr   gler wrote:
> > On 1 Mrz., 19:05, german diago <germandi...@gmail.com> wrote:
> >> This works and compiles. Now I would like to know if it's possible to
> >> do something in the lines of (which doesn't work as of now for me):
>
> >> template ...
> >> class MyClass {
> >>     klass * object_;
>
> >>     typedef decltype((object_->*PointerToMemberFunc)()) ReturnType;
> >> };
>
> >> As you can see, there are two parameters that are called the same way.
> >> One of them is a pointer to member function type, the other is an
> >> address to one of those types. I don't know how to disambiguate this.
> >> Can anyone help, please? Thanks in advance.
>
> [snipped]
>
> > I do not understand your comment regarding disambiguation,
> > there is no problem with PM at all. The problem is
> > related to the fact that you cannot refer to the member
> > object_ in the decltype type specifier. This is quite clear,
> > because you are not in the body of member function
> > here and outside of that scope is not defined (You
> > refer to this indirectly via object_ which is equivalent to
> > this->object_). This problem is easy to fix:
>
> I was in the impression that C++0x fixes this. In fact, GCC trunk already
> accepts code like
>
> struct A { int a; decltype(a + 1.0) b; };
>
> According to 5.1.1/10 (last bullet) in n3035. "this" is not added if not in
> the body of a non-static member function.

You are right, the OP's code was well-formed according to
the recent working draft. [class.mfct.non-static]/3 makes it
specifically clear, that "a" is not transformed into "(*this).a"
in this context.

Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]