Search code examples
c++templatestypescastingtypedef

How can I make a template resolve type only by first parameter?


How can I have a function like this

template<typename TValue>
TValue GridSnap(TValue Value, TValue Grid)

That resolves template type from only the first parameter like this

GridSnap(ADouble, AFloat)  => TValue should resolve to double
GridSnap(AFloat,  ADouble) => TValue should resolve to float
GridSnap(AFloat,  AInt)    => TValue should resolve to float

basically I don't want to manually cast the second argument, it would be nice if it could just do implicit cast base on the first parameter.


Solution

  • @NathanOliver solution worked nicely, But I also found this solution

    TValue GridSnapAuto(TValue Value, decltype(Value) Grid)