Constexpr for Point constructors and many of its methods

I’d suggest adding constexpr specifiers for most of the Point class–all those functions and constructors that meet the C++11 requirements for the specifier–pretty much all of the functions that contain only a return statement and the constructors.

Reason–juce::Point objects are used for comparison and a number of static objects are defined in the ResizableLayout files. Those all can then be specified as constexpr which could speed up execution a bit.

Annoyingly we still support VS2013, and that doesn’t support constexpr for functions!

It’s a good point though, we probably have hundreds of functions that could be made constexpr. In practice I don’t think it’d make much difference to performance, because the compiler shouldn’t have any problem optimising almost all of these simple ones away, but it’d be nice for people to be able to call them from their own constexpr functions.

I guess an option for us would be to add an ugly JUCE_CONSTEXPR macro that we can use, which would work on anything other than VS2013.

Can’t you do the same thing you do with override and just #define constexpr to be nothing for VS2013? that seems nicer than having to make a JUCE_CONSTEXPR definition and it means anyone writing code with constexpr will be able to compile on VS2013 if they want to.

No, because VS2013 does understand the constexpr keyword, but it only has a partial implementation of its functionality.


JUCE_CONSTEXPR as ugly as it is, would be a nice interim solution. Once you stop supporting VS2013 it’ll be a quick search and replace to get rid of and replace with constexpr, and it’ll allow you to work the specifier incrementally as things get updated.

I agree it’s not a huge benefit, but the accumulation of little improvements can help make a difference overall.

On a side note: how many people are still using VS2013? And why? Seems odd.

You can even throw in a JUCE_CONSTEXPR14 for those compilers that support the c++14 extension to the constexpr specifier.

There’s still a bit of VS inertia. For example the AAX SDK comes with VS2013 as the most recent project type.

JUCE_CONSTEXPR and JUCE_HAS_CONSTEXPR have now been added to JUCE on develop with commit 4a2a529.

1 Like