constexpr parameters

109 views
Skip to first unread message

Noah Roberts

unread,
Oct 22, 2014, 2:10:06 AM10/22/14
to

Where there insurmountable technical reasons why C++ doesn't allow you
to accept constexpr function parameters? In other words:

constexpr int sq(constexpr int i) { return i * i; }

This in combination with constexpr member function overloads like so:

struct whatnot
{
constexpr int fun() constexpr;
};

I ask because I think this could aleviate most if not all roadblocks to
real constexpr metaprogramming and would let them do a lot more than TMP
besides. You could for example reverse constexpr strings or turn them
into types (with mpl::string or something). I'm imagining something
like so:

template < size_t Len >
struct cstring
{
// postfix constexpr saying can only use on constexpr instance.
constexpr char operator [] (constexpr size_t i) constexpr;

// more complex substr stuff...also constexpr everywhere.
};

template < typename ... IChar >
struct tstring : boost::mpl::string<IChar...>
{
// doesn't access data members so no need for postfix
static constexpr auto prepend(constexpr char c)
{
return tstring<c,IChar...>();
}
};

constexpr tstring<> to_type(constexpr cstring<0>)
{
return tstring<>();
}

template < size_t Len >
constexpr auto to_type(constexpr cstring<Len> str)
{
return to_type(str.substr(1)).prepend(str[0]);
}

The motivating thing was working with this constexpr string type I made
and really wishing I could make them unique types in addition to their
current powers:
https://github.com/crazy-eddie/crazycpp/tree/master/20141016-constexprstr

Some of the above could be done with things like integral_type<char, c>
but I don't see a way around passing cstring<Len> as a parameter and
hoping to treat it as constexpr.

Before I attempted to go the whole proposal route I wanted to know if
this kind of thing had been considered already and discarded due to
difficulty in implementation or some other concern. It seems on its
face to make sense but I've never had to implement an implementation
before.


--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Reply all
Reply to author
Forward
0 new messages