Re: Function versus pointer to function, in context of std::function, huh?
On 26 Jul., 17:49, Alf P. Steinbach wrote:
On 26.07.2011 14:39, SG wrote:
*(WNDPROC()) is not a name but an lvalue expression referring to a
function. As such, decltype is forced to yield an lvalue-reference-to-
function type which is also not viable as template parameter for
std::function<>.
That sounds rather impractical and at odds with the usual "reference
can't be detected".
It's more powerful than typeof.
I also assume that good old `typeof` (from e.g. g++) was more practical.
I agree that in many cases the behaviour of the typeof extension is
all you need. And in some cases it won't. (Think of how to implement
result_of<>). I expect to see many decltype uses similar to this:
template<class T, class U>
auto operator*(vec3<T> const& v, U s) -> vec3<decltype(p[0]*s)>;
where replacing decltype with typeof ought not to make a difference.
And if you really need the typeof behaviour very often, you may use
this shortcut:
#include <type_traits>
#define TYPEOF(xpr) typename std::decay<decltype(xpr)>::type
Since C++2011 allows the typename keyword to appear in more contexts
(including non-template ones) this macro should just work everywhere.
I'm just starting to use the C++0x stuff,
it doesn't seem that well-designed.
There are some little warts here and there, but all in all I'm very
happy with how things turned out.
So, does one have to use some template stuff like
PointeeType<WndProc>::T, where a reader of code must look elsewhere for
definition, or can decltype be brought into service via some proper
black magic incantantion?
I don't think so.
but anyway, why should the
programmer have to write that when the template could/should do it?
I don't see the value in this. You'd end up with distinct but
equivalent types with the exact same purpose. In your case:
function<void(*)()> and function<void()>.
You mention that as if it were some kind of problem?
What problem do you see with that?
It's the principle of the matter. You end up with two different types
that could have been one. It complicates things. Will a
function<void(*)()> be convertible to a function<void()> and/or the
other way around? If not why not? Also, the types are not reference-
compatible.
Since we're dealing with such equivalence in practice anyway, e.g. decay
to pointer type everywhere a function type is used as formal argument
type, I really don't see any problem.
I really don't see any advantage. And I really don't like the let's-
just-use-two-distinct-types-for-the-same-thing strategy. :-p
If you hate his so much you could write a proposal to give
std::function some other implementation-defined name (i.e.
std::_function_) and to create a type-transforming alias that emulates
this decay for types:
template<class T> struct aftt_ { typedef T type; };
template<class T> struct aftt_<T*> : aftt_<T> {};
template<class T> struct aftt_<T&> : aftt_<T> {};
template<class T> using function = _function_<typename
aftt_<T>::type>;
With this, function<void()> and function<void(*)()> would be
synonymous with _function_<void()> and all these names would refer to
exactly one type (and we both can live in peace). ;-)
[...opinion...]
That's yours. I have mine.
Cheers!
SG