reference_wrapper
and function typesSection: 22.10.6 [refwrap] Status: C++11 Submitter: Howard Hinnant Opened: 2009-02-18 Last modified: 2016-01-28
Priority: Not Prioritized
View all other issues in [refwrap].
View all issues with C++11 status.
Discussion:
The synopsis in 22.10.6 [refwrap] says:
template <ObjectType T> class reference_wrapper ...
And then paragraph 3 says:
The template instantiation
reference_wrapper<T>
shall be derived fromstd::unary_function<T1, R>
only if the typeT
is any of the following:
- a function type or a pointer to function type taking one argument of type
T1
and returningR
But function types are not ObjectType
s.
Paragraph 4 contains the same contradiction.
[ Post Summit: ]
Jens: restricted reference to ObjectType
Recommend Review.
[ Post Summit, Peter adds: ]
In https://svn.boost.org/trac/boost/ticket/1846 however Eric Niebler makes the very reasonable point that
reference_wrapper<F>
, whereF
is a function type, represents a reference to a function, a legitimate entity. Soboost::ref
was changed to allow it.http://svn.boost.org/svn/boost/trunk/libs/bind/test/ref_fn_test.cpp
Therefore, I believe an alternative proposed resolution for issue 987 could simply allow
reference_wrapper
to be used with function types.
[ Post Summit, Howard adds: ]
I agree with Peter (and Eric). I got this one wrong on my first try. Here is code that demonstrates how easy (and useful) it is to instantiate
reference_wrapper
with a function type:#include <functional> template <class F> void test(F f); void f() {} int main() { test(std::ref(f)); }Output (link time error shows type of
reference_wrapper
instantiated with function type):Undefined symbols: "void test<std::reference_wrapper<void ()()> >(std::reference_wrapper<void ()()>)",...I've taken the liberty of changing the proposed wording to allow function types and set to Open. I'll also freely admit that I'm not positive
ReferentType
is the correct concept.
[ Batavia (2009-05): ]
Howard observed that
FunctionType
, a concept not (yet?) in the Working Paper, is likely the correct constraint to be applied. However, the proposed resolution provides an adequate approximation.Move to Review.
[ 2009-05-23 Alisdair adds: ]
By constraining to
PointeeType
we rule out the ability forT
to be a reference, and call in reference-collapsing. I'm not sure if this is correct and intended, but would like to be sure the case was considered.Is dis-allowing reference types and the implied reference collapsing the intended result?
[ 2009-07 Frankfurt ]
Moved from Review to Open only because the wording needs to be tweaked for concepts removal.
[ 2009-10-14 Daniel provided de-conceptified wording. ]
[ 2009-10 post-Santa Cruz: ]
Move to Tentatively Ready.
Proposed resolution:
Change 22.10.6 [refwrap]/1 as indicated:
reference_wrapper<T>
is aCopyConstructible
andCopyAssignable
wrapper around a reference to an object or function of typeT
.