Re: pair, tuple and constexpr
On Nov 12, 10:22 pm, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
template <class T>
struct call_traits
{
typedef typename conditional<
is_literal<T>,
Daniel-the-nit-picker:
is_literal<T>::value,
(Yes, this is a slight asymmetry relative to the boost naming
convention)
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. */
};
Yes, good idea! I even think that the templated c'tor can be
implemented, but in this case we cannot use call_traits and
must instead two (aaarrg, 4) mutually exclusive c'tor variants
(via concepts or SFINAE), because of the otherwise impossible
argument deductivity (Resolve T given call_traits<T>::type).
I don't want to conceal one downer of std::is_literal: Actually a such
nominated literal type is just an optimistic categorization and
not binding, because a literal type can *also* have non-constexpr
c'tors. If we consider this example
struct Literal {
constexpr Literal(int);
Literal(const std::string&);
};
std::pair<Literal, int> p(Literal("no literal"), 42);
this would invoke the c'tor taking arguments by value (because
Literal itself is a literal type), although the correspondingly
called c'tor of std::pair cannot be a constexpr c'tor due to the
non-constexpr-compatible argument of type Literal. Does this
harm? Probably not, but it depends on the resolution of
what you mention here:
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").
Greetings from Bremen,
Daniel
---
[ 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 ]