Maurizio Tatafiore
Maurizio Tatafiore

Reputation: 21

Template parameter involved in multiplication is cast to float

Sorry for the unclear title but I really don't know how to express it.

I have a CurveSegment class which does interpolation on a generic parameter and it is structured like this:

template<class T>
class CurveSegment
{
public:
    inline void interpolate(float t)
    {
        mCurrentValue = mV0 + mSpan * mInterpolator->interpolate(t);
    }

private:
    T mCurrentValue;
    T mV0;
    T mSpan;
}

mInterpolator->interpolate(t) returns a float.

I was kinda assuming that, if T has overloaded multiplication and addition operators with a float rhs, the compiler would infer that at instance level and do the operations using the type T. Instead I get the warning "=: conversion from float to T, possible loss of data", so I imagine that everything gets cast to a float before assignment and I clearly don't know something about the rules involved.

What am I missing here?

Upvotes: 1

Views: 53

Answers (1)

Maurizio Tatafiore
Maurizio Tatafiore

Reputation: 21

I think @Jarod42 in the comments has pointed out exactly where the warning is coming from:

if T is int you have int = int + int * float. > int * float -> float; int + float -> float; > > int = float -> warning.

I was testing with simple types, int included, so here is the reason for this warning. But this shows a flaw in how I structured this code, so I'm going to rethink it.

Thanks everyone for the quick assistance.

Upvotes: 1

Related Questions