CppCon 2019: David Stone - Removing Metaprogramming From C++, Part 1 of N: constexpr Function Params
Video Statistics and Information
Channel: CppCon
Views: 11,901
Rating: undefined out of 5
Keywords: David Stone, CppCon 2019, Computer Science (Field), + C (Programming Language), Bash Films, conference video recording services, conference recording services, nationwide conference recording services, conference videography services, conference video recording, conference filming services, conference services, conference recording, event videographers, capture presentation slides, record presentation slides, event video recording, video services
Id: bIc5ZxFL198
Channel Id: undefined
Length: 60min 7sec (3607 seconds)
Published: Fri Oct 11 2019
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
absolutely incredible proposal! hope you will make it into C++23
One thing seems silly to me.
If you know you are in a constexpr context with if(std::is_constant_evalutated()), or you are in a consteval function, then you know all of the arguments that got you there are compile time constants. Surely the compiler should permit you to use these arguments as if they are constexpr.
Essentially, in all consteval functions, all parameters should also be assumed to be consteval, without having to be declared as such. Inside the scope of the if block of if(std::is_constant_evalutated()), all parameters should be assumed to be consteval, without them having to be declared as such.
You already are using this logic with the proposed is_constant_expression(variable). Technically some variable that may be runtime cannot be assumed to be compile time unless checked by an if block, just as with what I propose with normal parameters to constexpr functions inside an is_constant_evaluated block.
Also, I don't think we necessarily need to break non-parametric constexpr variables by making them maybe constexpr. We can still have constexpr parameters being maybe constexpr (since it is part of a function signature), and consteval parameters being definitely constexpr. There is always the possibility in future to relax constexpr variables to being maybe constexpr outside a function parameter context, but it's definitely a breaking change to do it now purely for ideological reasons.
Maybe-constexpr variables are a hassle anyway, because you always have to prove later that they are indeed constexpr if you want to use them in a compile time context. It only really serves a purpose as an alternative to parametric overload in my mind, where overloads are defined by if block, as with if constexpr and if is_constant_evaluated.
Perhaps if we get epochs, we can do the nice consistent thing and make constexpr variables maybe-constexpr.
I have a question about the proposed new meaning of constexpr for variables at 53:41:
To me this sounds like the compiler would have to look at how the variable is being used to decide if it should do the initialization at compile time, but is this really the case? What makes more sense is if it tried to evaluate it at compile time and fell back to runtime if it failed like is already the case for const integers and global variables. In that case, wouldn't it be more correct to say: "done at compile time if possible, ..."? I know it breaks the symmetry but to me this makes more sense if this is how it works.
I think his discordia analogy for shadow worlds is wrong. In discordia, things sometimes work the opposite of how they are supposed to. In the shadow worlds, you sometimes lack things that the main world would have. Some examples:
I like having different notation for values that should be known at compile time rather than call time as they will get treated differently by the compiler and have different effects and get modified in different ways. Stripping this out makes it harder to figure out what is going on when.
I think the main reason TMP is difficult is that if you are having bugs in TMP, this likely prevents a successful compile and that is (outside of TMP) usually the easiest hurdle in getting to a successful program. As a consequence it feels draining in a standup when discussing that you are not even at a compile yet. TBH, the thing it needs the most is the ability to printout simpler diagnostic messages.
I think he makes a mistake in one of the questions in the talk: https://www.youtube.com/watch?v=bIc5ZxFL198&t=3445s
Wouldn't that be a bracket operator that's
constexpr
with a parameter that's `consteval`? For a tuple, the index parameter would have to be a compile time constant in order for the return type of the function to be known at compile time. And the operator itself would beconstexpr
because it could only be compile time constant if the implicitthis
was also compile time constant. Perhaps that means we need another decorator keyword at the end of member functions in addition to const, final, etc... in order to talk about the constexpr-ness of this.I kind of agree that maybe
constexpr
should just be assumed everywhere. It does seem like kind of a mistake. I don't think though that we could've figured out it was a mistake without trying it first though.There should never be a need to mark anything as constexpr. That should be determinable at compile time by the compiler and simply evaluated then.
A compiler can detect purity and const ness and if so evaluate all such functions at compile time if possible.
I'm still a bit confused and couldn't find info anywhere else, but is this for C++20? Or not?