Re: Passing "empty" types as parameters
On 14 Sep., 00:28, AJG <plus....@gmail.com> wrote:
[..]
1) I might need to be able to print (or more specifically, "skip")
this empty value, e.g. because it can form part of a compound type
such as a tuple<>. Thus I'll need an operator << for it. E.g.:
std::ostream& operator <<(std::ostream& o, void_ const&) { return o; =
=
}
First, I would really *not* name this parameter "_void" - IMO
this leads to similar confusion like i versus l, plus the
semantic confusion. I think that names like "empty" or "none"
are better here, but of-course this is only my personal
preference.
Second: Yes, I agree that such use-cases make sense and I
really don't see any reason for *not* using by-value call
convention here.
2) I have a function that returns its parameter (so, more or less the
identity function), except when the parameter is "empty," in which
case I need a different default-constructed type. So, something like:
template <typename T>
T const& t_or_default(T const& t) { return t; }
result_type t_or_default(void_ const&) { return result_type(); }
Thus my question stands: what's the best way to accept those empty types?
I would provide them by value, it's probably the most
reasonable calling convention for empty (or "small")
types, unless you have to satisfy other restricting
constraints, e.g. when specializing a function template
which already uses a function parameter/return type
that is a reference type as in the following variant:
template<>
const void_& t_or_default(void_ const& arg) { return arg; }
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]