What the default behavior should be

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

What the default behavior should be

Post by albinopapa » September 19th, 2019, 3:58 pm

Compiler support list for different versions of C++.

After reading through some of the list and reading some of the papers/proposals, it seems to me, that people really want most if not all the restrictions of constexpr to go away. It really would make sense to have all functions to be constexpr by default without the need to decorate them, like lambdas. Not all functions can be evaluated AS a constant expression, but even in functions not evaluated as a constant expression, you are restricted to only calling constexpr decorated functions from within constexpr decorated functions. A function not decorated with constexpr can call any other function. A function decorated with constexpr can only call other functions decorated with constexpr.

In expressions that are not constant expressions, the constexpr functions act like normal functions with a few restrictions. No allocations, no calling functions not declared constexpr, no try/catch, no changing active union members, etc...

A few restrictions on constexpr have already been removed since it's inception in the C++11 standard. I'd like to see constexpr be set aside just for variable declarations, which force compile time evaluation of any function IF it can be evaluated at compile time...like lambdas. I realize that lambdas have a huge benefit over normal function in that the compiler can "see what's going on" with a lambda since it's right next to the code it's using at the moment. If that's the road block, then any function that is able to be inlined, should be considered to be a constexpr function.

I think that's already a prerequisite for a constexpr function. I've tried splitting the declaration and definition into separate files, and the compiler isn't able to "see" the definition. I haven't tried declaring and defining in header ( declare at top, define below ).

Any thoughts?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: What the default behavior should be

Post by chili » September 26th, 2019, 4:39 pm

Theoretically I guess it seems possible. Mostly it's probably just really hard to implement. Constraining what can and cannot be executed at compile time makes the implementer's job attainable, and adding the constexpr keyword enables the compiler to verify that your intentions are possible to carry out.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: What the default behavior should be

Post by albinopapa » September 26th, 2019, 9:13 pm

C++20 will bring us consteval, after that I don't think the constexpr function declaration will have much need.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply