Re: pair, tuple and constexpr
Daniel Kr=C3=BCgler ha scritto:
On Nov 12, 1:59 am, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
Let's rephrase my question, then: would it make sense to modify the
current definition of pair and/or tuple so that they could be literal
types whenever all the template arguments are of literal type? For
example, the current std::pair constructor could be defined as
constexpr pair(/implementation defined/ x, /implementation defined/ y)=
;
where the type of x is T1 if T1 is a literal type or const T1&
otherwise, and similarly for T2. (Notice that this kind of
"implementation defined" signature is already being used in the
standard, see for example the constructor of unique_ptr).
Personally I think this is a good reason, see e.g. my use-case
of a function template abs both useful in constant expressions
and otherwise, the branch based on an hypothetical std::is_literal,
see
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#719
:-) It definitely seems that we reached the same conclusions! std::pair
could be written with std::is_literal in this way:
template <class T>
struct call_traits
{
typedef typename conditional<
is_literal<T>,
T,
typename add_lvalue_reference<
typename add_const<T>::type
>::type
>::type type;
};
template <class T1, class T2>
struct pair
{
T1 first;
T2 second;
constexpr pair(
typename call_traits<T1>::type a,
typename call_traits<T2>::type b)
: first(a), second(b)
{}
/* etc. */
};
Of course, this definition assumes that constexpr is silently ignored if
either T1 or T2 is not a literal type (see discussion in thread "Defect
report: [dcl.constexpr]/5 constexpr and templates").
Cheers,
Ganesh
PS: compare this use case with boost::call_traits
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]