Topic: c++0x: is it possible to deduce return type in this way?


Author: german diago <germandiago@gmail.com>
Date: Thu, 25 Feb 2010 17:09:31 CST
Raw View
Hello. I have a class, with getters and setters.

I want to deduce the return type of the getters.

They can be const references (if it's a property held in memory) or
values (if they're not stored in memory)

I would like to be able to deduce, from another context, the return
type of a function.

The problem is the following one:

class Employee {
      float getSalary() const;

};


I want to know if it's possible (using c++0x features)
to do something similar to this:


template <class ObjectType>
struct GetReturnType {
     ObjectType * object_;


     template <class PropType,
                const PropType & (ObjectType::*Getter)() const,
                class E = typename EnableIf<is_lvalue_reference<
                                         decltype((object_->*Getter)())
                >::value>::type *>
     struct ForGetter {
         typedef decltype ((object_->*Getter)()) type;
     };


     /*
     template <class PropType,
                PropType (ObjectType::*Getter)() const,
                class = typename DisableIf<is_lvalue_reference<
                                         decltype((object_->*Getter)())
                >::value>::type *>
     struct ForGetter {
         typedef decltype ((object_->*Getter)()) type;
     };
     */
};

Usage:

typename ReturnType<Employee>::ForGetter<&Employee::getSalary>::type

I can't "overload" the class via SFINAE, which is exactly what I want.
Is there any trick to do it?
Thanks.






--
[ 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: Mathias Gaunard <loufoque@gmail.com>
Date: Fri, 26 Feb 2010 12:35:03 CST
Raw View
On Feb 25, 11:09 pm, german diago <germandi...@gmail.com> wrote:

> I can't "overload" the class via SFINAE, which is exactly what I want.
> Is there any trick to do it?

There is no such thing as overloading of classes.
What you're looking for is partial specialization. It works fine with
SFINAE.


--
[ 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: german diago <germandiago@gmail.com>
Date: Sat, 27 Feb 2010 20:34:44 CST
Raw View
On 26 feb, 19:35, Mathias Gaunard <loufo...@gmail.com> wrote:
> On Feb 25, 11:09 pm, german diago <germandi...@gmail.com> wrote:
>
> > I can't "overload" the class via SFINAE, which is exactly what I want.
> > Is there any trick to do it?
>
> There is no such thing as overloading of classes.
> What you're looking for is partial specialization. It works fine with
> SFINAE.

Thanks. It's the solution I came with finally. It works great!

--
[ 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                      ]