std::declval?

60 views
Skip to first unread message

Matthew Dempsky

unread,
Dec 18, 2015, 6:46:30 PM12/18/15
to cxx
Is std::declval allowed?  It's not mentioned on chromium-cpp.

It's useful in conjunction with decltype for computing the types of constructed expressions when you don't already have objects of a particular type to reference.  E.g.:

// ResultType<F, Args...> is the return type from applying a functor of type F
// to an arguments list of type Args....
template <typename F, typename... Args>
using ResultType = decltype(std::declval<F>()(std::declval<Args>()...));

Jeremy Roman

unread,
Dec 18, 2015, 7:03:48 PM12/18/15
to Matthew Dempsky, cxx
Your example also looks a lot like std::result_of to me.

On Fri, Dec 18, 2015 at 6:46 PM, Matthew Dempsky <mdem...@chromium.org> wrote:
Is std::declval allowed?  It's not mentioned on chromium-cpp.

I think since it's not yet mentioned, probably "no".

Assuming there are no issues with any of the compilers/stdlibs we use (I doubt it -- the implementation of declval is very simple), I wouldn't oppose permitting it, but usage should be rare.

Is there a concrete place you'd like to use std::declval?
 
It's useful in conjunction with decltype for computing the types of constructed expressions when you don't already have objects of a particular type to reference.  E.g.:

// ResultType<F, Args...> is the return type from applying a functor of type F
// to an arguments list of type Args....
template <typename F, typename... Args>
using ResultType = decltype(std::declval<F>()(std::declval<Args>()...));

Your example looks a lot like std::result_of to me. There is a lot of subtlety around result_of (see, for instance, https://youtu.be/zt7ThwVfap0?t=12m13s), so we need to be careful.

Matthew Dempsky

unread,
Dec 18, 2015, 7:09:00 PM12/18/15
to Jeremy Roman, cxx
On Fri, Dec 18, 2015 at 4:03 PM, Jeremy Roman <jbr...@chromium.org> wrote:
Your example also looks a lot like std::result_of to me.

Aha, indeed it does.  (I checked <type_traits> before emailing, but I guess I missed it.)

Is there a concrete place you'd like to use std::declval?

My immediate motivation was just writing that ResultType template and couldn't find std::declval listed in chromium-cpp.  Since declval and <type_traits> are allowed, it seems like we might as well allow std::declval for completeness (with the same "Usage should be rare" disclaimer).

Jeremy Roman

unread,
Jan 7, 2016, 5:02:54 PM1/7/16
to Matthew Dempsky, cxx
To close the loop here: the feature looks fine. If/when someone has a good use case and a CL that passes trybots (proving it works on all platforms), we can change chromium-cpp accordingly.
Reply all
Reply to author
Forward
0 new messages