[REQUEST] float * Point<float> overload


#1

Point<T> defines * for (Point<T>, T) but not the other way round. So myScalar*myVector will fail to compile.

Could we have bidirectional? So that one could transfer something like:

x = α u + β v

... directly to code?

π


#2

why can't you just subclass Point and add your own overloaded operators? 


#3

I don't really mind using {vector}{OPERATOR}{scalar}.

Just.. I would expect someone to expect the other way round.

I've been posting minutiae (e.g. where I expect A but the framework gives B) on-the-fly in case some of the feedback might be of some use.

π


#4

Yeah, this is a good request, and something I'd been meaning to do. Will add it when I get a moment!


#5

This is actually a pain in the donkey.

A naive approach...

template <typename T, U> Point<T> operator* (U multiplier, Point<T>point) noexcept { ...

... fails as {Rect}*{Point} snaffles the overload.

 Trying to get cunning with SFINAE http://coliru.stacked-crooked.com/a/1e226e96fc435f2a fails on Windows due to MSVC being a persistent pile of shit (https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/).

The least awkward method I can see is to temporarily promote the scalar to a double:

template <typename ValueType>
class Point
{
public:
    :
    static friend Point  operator* (double multiplier, Point<ValueType>point) noexcept {
        return Point((ValueType)(point.x * multiplier), (ValueType)(point.y * multiplier)); 
    }

or:

template <typename T>
Point<T> operator* (double multiplier, Point<T>point) noexcept {
    return Point<T>((T)(point.x * multiplier), (T)(point.y * multiplier));
}

I suppose I would prefer the former, as it keeps the overload within Point's declaration.

π

PS I would also consider macros to avoid duplicating otherwise identical code for each operator.


#6

I already pushed something this morning.