Are parameters of constexpr! functions necessarily themselves constant expressions?

69 views
Skip to first unread message

Brian Bi

unread,
Nov 4, 2018, 3:15:12 PM11/4/18
to std-pr...@isocpp.org
P1073R1 introduces immediate functions: functions declared with the constexpr! specifier, which forces all invocations to be constant expressions. I wonder whether it might make sense to allow the following type of code:

template <int> struct Foo {};
constexpr! void bar(int x) { Foo<x> foo; }

--
Brian Bi

Nicolas Lesser

unread,
Nov 4, 2018, 3:27:23 PM11/4/18
to std-pr...@isocpp.org
Hey I had the exact same question several months ago! :)

So I asked around and as it turns out this is very problematic for the compiler. When evaluating functions as constant expressions, the compiler switches to like an "interpreter" mode where no template instantiations can be performed. Because of templates, invoking the function with different parameters can get different results.

One related problem with this is that this would mean that the function for all intents and purposes just like a template is: You can possibly get different functions from a single function.

constexpr parameters also suffer from this problem.
So, this was an explicit design decision AFAIK.

P.S. it's consteval now :)

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAMmfjbOT8toNhvzWc9NoCrb0G3DYOzc%2BRZ9yZZrd6jtJDs1G%3DQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages