[typeof] deducing type at class level

Hello all, Is there any way to use BOOST_TYPEOF at class level? For example: struct vect { size_t size(); typedef BOOST_TYPEOF(size()) size_type; // can I make this work somehow? }; If not with Boost.Typeof, can I do this with C++0x? Thanks! --Lorenzo -- View this message in context: http://boost.2283326.n4.nabble.com/boost-typeof-deducing-type-at-class-level... Sent from the Boost - Dev mailing list archive at Nabble.com.

On 22/06/2011 18:02, lcaminiti wrote:
Hello all,
Is there any way to use BOOST_TYPEOF at class level? For example:
struct vect { size_t size();
typedef BOOST_TYPEOF(size()) size_type; // can I make this work somehow?
typedef BOOST_TYPEOF(make<vect>().size()) size_type; with #ifndef BOOST_NO_RVALUE_REFERENCES template<class T> typename enable_if< is_reference<T>, T
::type make();
template<class T> typename disable_if< is_reference<T>, T&
::type make();
#else T&& make(); #endif

On 22 June 2011 18:18, Mathias Gaunard <mathias.gaunard@ens-lyon.org> wrote:
On 22/06/2011 18:02, lcaminiti wrote:
Hello all,
Is there any way to use BOOST_TYPEOF at class level? For example:
struct vect { size_t size();
typedef BOOST_TYPEOF(size()) size_type; // can I make this work somehow?
typedef BOOST_TYPEOF(make<vect>().size()) size_type;
with
#ifndef BOOST_NO_RVALUE_REFERENCES
template<class T> typename enable_if< is_reference<T>, T
::type make();
template<class T> typename disable_if< is_reference<T>, T&
::type make();
#else
T&& make();
#endif
This can be done using boost::type_traits::add_reference I believe: template <class T> typename add_reference<T>::type make(); Jeroen

On 22/06/2011 18:42, Jeroen Habraken wrote:
This can be done using boost::type_traits::add_reference I believe:
template<class T> typename add_reference<T>::type make();
Actually there is a declval somewhere in boost that more or less does this, unfortunately it returns by value in C++03, which is a big no-no for me, since it restricts it to copyable types.

On Wed, Jun 22, 2011 at 10:00 AM, Mathias Gaunard < mathias.gaunard@ens-lyon.org> wrote:
On 22/06/2011 18:42, Jeroen Habraken wrote:
This can be done using boost::type_traits::add_**reference I believe:
template<class T> typename add_reference<T>::type make();
Actually there is a declval somewhere in boost that more or less does this, unfortunately it returns by value in C++03, which is a big no-no for me, since it restricts it to copyable types.
[At the risk of drifting off-topic...] If you're worried about that, shouldn't you be using declval<T&>() if you really want an lvalue? - Jeff

AMDG On 06/22/2011 12:40 PM, Jeffrey Lee Hellrung, Jr. wrote:
On Wed, Jun 22, 2011 at 10:00 AM, Mathias Gaunard < mathias.gaunard@ens-lyon.org> wrote:
On 22/06/2011 18:42, Jeroen Habraken wrote:
This can be done using boost::type_traits::add_**reference I believe:
template<class T> typename add_reference<T>::type make();
Actually there is a declval somewhere in boost that more or less does this, unfortunately it returns by value in C++03, which is a big no-no for me, since it restricts it to copyable types.
[At the risk of drifting off-topic...] If you're worried about that, shouldn't you be using declval<T&>() if you really want an lvalue?
Yeah. Returning by value is the only way to get an rvalue in C++03. In Christ, Steven Watanabe

On Wed, Jun 22, 2011 at 4:08 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
On 06/22/2011 12:40 PM, Jeffrey Lee Hellrung, Jr. wrote:
On Wed, Jun 22, 2011 at 10:00 AM, Mathias Gaunard < mathias.gaunard@ens-lyon.org> wrote:
On 22/06/2011 18:42, Jeroen Habraken wrote:
This can be done using boost::type_traits::add_**reference I believe:
template<class T> typename add_reference<T>::type make();
Actually there is a declval somewhere in boost that more or less does this, unfortunately it returns by value in C++03, which is a big no-no for me, since it restricts it to copyable types.
[At the risk of drifting off-topic...] If you're worried about that, shouldn't you be using declval<T&>() if you really want an lvalue?
Yeah. Returning by value is the only way to get an rvalue in C++03.
In Christ, Steven Watanabe
Thanks to all. What you have suggested works for me (I'm not worried about r/lvalues). --Lorenzo

On 22/06/2011 18:18, Mathias Gaunard wrote:
#ifndef BOOST_NO_RVALUE_REFERENCES
template<class T> typename enable_if< is_reference<T>, T
::type make();
template<class T> typename disable_if< is_reference<T>, T&
::type make();
#else
T&& make();
#endif
Sorry, a template<class T> is missing and it should be ifdef not ifndef, but you get the idea.

Mathias Gaunard-2 wrote:
On 22/06/2011 18:02, lcaminiti wrote:
Hello all,
Is there any way to use BOOST_TYPEOF at class level? For example:
struct vect { size_t size();
typedef BOOST_TYPEOF(size()) size_type; // can I make this work somehow?
typedef BOOST_TYPEOF(make<vect>().size()) size_type;
with
#ifndef BOOST_NO_RVALUE_REFERENCES
template<class T> typename enable_if< is_reference<T>, T
::type make();
template<class T> typename disable_if< is_reference<T>, T&
::type make();
#else
T&& make();
#endif
Hi, note that there is already declval in the standard ans in Boost.Utility taht can be used instead of make. 20.2.4 Function template declval [declval] 1 The library provides the function template declval to simplify the definition of expressions which occur as unevaluated operands (Clause 5). Best, Vicente -- View this message in context: http://boost.2283326.n4.nabble.com/boost-typeof-deducing-type-at-class-level... Sent from the Boost - Dev mailing list archive at Nabble.com.
participants (7)
-
Jeffrey Lee Hellrung, Jr.
-
Jeroen Habraken
-
lcaminiti
-
Lorenzo Caminiti
-
Mathias Gaunard
-
Steven Watanabe
-
Vicente Botet