mobius3 / tweeny

A modern C++ tweening library
http://mobius3.github.io/tweeny
MIT License
742 stars 53 forks source link

Conflicting type when using jump function #21

Closed grimaldini closed 3 years ago

grimaldini commented 3 years ago

When trying to compile a simple hello world using the jump function, I'm getting "4. Candidate template ignored: deduced conflicting types for parameter 'T' ('int' vs. 'unsigned long')":

*: error: no matching function for call to 'clip'
        p = detail::clip(p, 0, points.size() -1);
            ^~~~~~~~~~~~
*: note: in instantiation of member function 'tweeny::tween<number, number, number, number, number, number>::jump' requested here
        tween.jump(0, true);
              ^
In file included from *:
In file included from *:
*/tweeny.h:1806:11: note: candidate template ignored: deduced conflicting types for parameter 'T' ('int' vs. 'unsigned long')
        T clip(const T & n, const T & lower, const T & upper) {
          ^

I'm using the stable single header file, all local paths have been replaced with *. Wondering if this might be due to vector.size() returning size_t that in some systems it might alias to different types.

Using a mac and Xcode

mobius3 commented 3 years ago

I'll try to reproduce the issue but from the looks of it, you are right.

Can you share which clang version is installed on your system (assuming you're using clang)?

grimaldini commented 3 years ago

Apple clang version 12.0.0 (clang-1200.0.32.29) Target: x86_64-apple-darwin20.2.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I was able to fix this by adding some explicit casting in

template<typename T, typename... Ts>
    inline const typename detail::tweentraits<T, Ts...>::valuesType & tween<T, Ts...>::jump(int32_t p, bool suppress) {
        p = detail::clip(p, 0, (int32_t)(points.size() -1)); // <- here
        return seek((int32_t)(points.at(p).stacked), suppress); // <- here
    }

I think you could also make p be a size_type here to avoid the casting and to optimize it for different architectures. Additionally, the same issue occurs in the interpolate function, to fix it you could do an explicit cast as below:

auto pointDuration = uint32_t(p.duration() - (p.stacked - (prog * static_cast<float>(total))));
mobius3 commented 3 years ago

Yeah, I was going to write the same solution here but you beat me to it :)

Considering points.size() will never be < 0 (under normal circumstances) and I'm already clipping p to be positive, might as well be a size_type indeed. Thanks for catching that.

mobius3 commented 3 years ago

I have adopted your suggestions. Thanks!