Topic: recursive constexpr
Author: Zachary Turner <divisortheory@gmail.com>
Date: Thu, 9 Jul 2009 12:33:01 CST Raw View
Is it possible for constexpr functions in C++0x to be recursive? it's
not immediately clear to me reading the proposal, and the closest
indication I can find is where it states "A constant-expression
function cannot be called before it is defined." which suggests that
maybe the answer is no. It depends on at what point the compiler
considers something as "defined" - at the opening curly brace, or at
the closing curly brace.
However, I think this would be quite powerful, as it would allow many
problems previously relegated to template metaprogramming to be
written in a more clear and consistent style. Consider, for example,
the well known compile time factorial:
template<int n>
struct factorial
{
enum { value = n * factorial<n-1>::value; }
};
template<>
struct factorial<0>
{
enum { value = 1; }
};
written as a constexpr function
constexpr int factorial(int n)
{
return (n==0) ? 1 : (n * factorial(n-1) );
}
Since the proposal also states "A constant-expression function may be
called with non-constant expressions,
in that case there is no requirement that the resulting value be
evaluated at compiletime." this means that for many types of
operations the programmer need not care about whether or not something
can be evaluated at compile time. He can write code to a single
interface and it will always select the most appropriate method
automatically.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Zachary Turner <divisortheory@gmail.com>
Date: Thu, 9 Jul 2009 14:15:43 CST Raw View
On Jul 9, 1:33 pm, Zachary Turner <divisorthe...@gmail.com> wrote:
> Is it possible for constexpr functions in C++0x to be recursive? it's
> not immediately clear to me reading the proposal, and the closest
> indication I can find is where it states "A constant-expression
> function cannot be called before it is defined." which suggests that
> maybe the answer is no. It depends on at what point the compiler
> considers something as "defined" - at the opening curly brace, or at
> the closing curly brace.
Yeesh, I should read more carefully next time. I missed the entire
page of the document that uses the factorial case as an actual example
and says it's not possible because the function isn't defined yet when
trying to be used inside of itself.
Apologies :)
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Thu, 9 Jul 2009 17:13:00 CST Raw View
On 9 Jul., 20:33, Zachary Turner <divisorthe...@gmail.com> wrote:
> Is it possible for constexpr functions in C++0x to be recursive?
Basically yes, because it falls under the special rule [expr.call]/9:
"Recursive calls are permitted, except to the function named main
(3.6.1)."
but...
> it's not immediately clear to me reading the proposal, and the closest
> indication I can find is where it states "A constant-expression
> function cannot be called before it is defined." which suggests that
> maybe the answer is no. It depends on at what point the compiler
> considers something as "defined" - at the opening curly brace, or at
> the closing curly brace.
... exactly this kind of problem, which is also handled by core issue:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#699
[Don't feel irritated by the issue title. The latter notes also refer
to
recursive constexpr functions in general]
> However, I think this would be quite powerful, as it would allow many
> problems previously relegated to template metaprogramming to be
> written in a more clear and consistent style.
There is no dispute about the usefulness of recursive constexpr
functions.
Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]