Summary: | std::result_of doesn't work | ||
---|---|---|---|
Product: | gcc | Reporter: | Piotr Wyderski <piotr.wyderski> |
Component: | libstdc++ | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED INVALID | ||
Severity: | normal | CC: | gcc-bugs, jwakely.gcc, piotr.wyderski |
Priority: | P3 | ||
Version: | 4.4.0 | ||
Target Milestone: | --- | ||
Host: | WinXP/x86/Cygwin | Target: | |
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: |
Description
Piotr Wyderski
2009-04-07 13:14:53 UTC
Note that *most* of the facilities in <functional> are still following the TR1 specifications, thus, in general, do not expect conformance to the latest C++0x draft (or file a DR for each unimplemented behavior ;) (In reply to comment #1) > Note that *most* of the facilities in <functional> are still following the TR1 > specifications, thus, in general, do not expect conformance to the latest C++0x > draft (or file a DR for each unimplemented behavior ;) > The only C++0x functionality involved I am aware of is decltype, which is outside of the template scope. BTW, boost::function_traits<...>::result_type works flawlessly. Here is a simplified testcase: #include <functional> int main(int argc, char *argv[]) { typedef std::result_of<int (long, float)>::type type; return 0; } Are you *really* sure the simplified testcase was supposed to work per the TR1 specifications? The author of thar code is Doug Gregor, and it never did in GCC. I'm looking at 3.4/3 in n1836, and my answer is *no*. So it is a purely C++0x bug, as you indicated in your first reply. Yes, a purely unimplemented C++0x feature. I think you have the syntax wrong, if you want to know the return type of a function type (or function pointer, or function reference) you need to say: result_of<int (*(long, float))(long ,float)>::type what you probably want is: typedef int (*func_ptr)(long, float); result_of<func_ptr(long, float)>:type Yes, that would work, we even have testcases for it. But what about C++0x, Jonathan? Seems strange that only that more convoluted syntax is right... This is not a bug in C++0x either. The spec is: Given an rvalue fn of type Fn and values t1, t2, ..., tN of types T1, T2, ..., TN in ArgTypes, respectively, the type member is the result type of the expression fn(t1, t2, ...,tN). So the original testcase has Fn=int and tries to find the result of calling int as a function, which is invalid. The original testcase should be: typedef std::result_of<decltype(fn) (long, float)>::type type; which tells you the result of invoking something of type decltype(fn) with arguments of type long and float. Or, to see what happens if you pass it other types: typedef std::result_of<decltype(fn) (int, double)>::type type; This tells you the result of calling e.g. f(0, 0.0), which will result in the arguments being implicitly converted, and as it's a simple function not a function object or other callable type with multiple overloads, the result is always 'int' I believe this is not a bug. Gosh, thanks Jonathan for the explanation, the key word for me is *overloading*, if overloading were not part of C++ these facilities would probably be more "intuitive" to use. Agreed, let's give Piotr two minutes to review your analysis and then let's close it. Subject: Re: std::result_of doesn't work 2009/4/7 jwakely dot gcc at gmail dot com <gcc-bugzilla@gcc.gnu.org>: > what you probably want is: In fact I want to copy the return type of a template method restore and use as another method's return type ina C++0x way. This works: class S { template <typename T> typename boost::function_traits<decltype(traits<T>::restore)>::result_type restore() { return traits<T>::restore(*this); } }; but the std::result_of-based soludins do not. The lambda-like syntax also does not work, because "this" is not available at the outer scope: template <typename T> auto restore() -> decltype(traits<T>::restore(*this)) { return traits<T>::restore(*this); } See what I wrote at http://gcc.gnu.org/ml/libstdc++/2008-09/msg00124.html under point 3) You want this: typename std::result_of< decltype(&traits<T>::restore) (S*) >::type Paolo, let's close this. (In reply to comment #12) > typename std::result_of< decltype(&traits<T>::restore) (S*) >::type Oops, that should be S& not S* It's not perfect, but for this case std::result_of works if you get the syntax right :) Agreed ;) Subject: Re: std::result_of doesn't work jwakely dot gcc at gmail dot com <gcc-bugzilla@gcc.gnu.org>: > You want this: > > typename std::result_of< decltype(&traits<T>::restore) (S*) >::type Thank you! :-) > Paolo, let's close this. Agreed. :-) Best regards, Piotr |