KDAB / KDAlgorithms

Algorithm wrappers
Other
82 stars 15 forks source link

reserve_helper type conversion warning #44

Closed hmoffatt closed 1 year ago

hmoffatt commented 1 year ago

I'm using kdalgorithms with Qt 5.15.12.

The following code generates warnings in Visual Studio about the type of size in reverse_helper.h:

    QVector<int> a{ 1,2,3 };
    auto b = kdalgorithms::filtered(a,
        [](const auto& i) { return i % 2; });
1>...\kdalgorithms\bits\reserve_helper.h(27,5): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data
1>...kdalgorithms\bits\reserve_helper.h(36,5): message : see reference to function template instantiation 'bool kdalgorithms::detail::reserve_helper<Container>(Container &,size_t,std::true_type)' being compiled
1>        with
1>        [
1>            Container=QVector<int>
1>        ]
jesperkdab commented 1 year ago

Qt5's QVector::reserve takes an int (See https://doc.qt.io/qt-5/qvector.html#reserve), while std's containers and Qt6 for that matter too takes a size_t (https://doc.qt.io/qt-6/qlist.html#reserve). Not sure what to do about it, besides maybe adding the size type to the template parameter:

template <typename Container, typename Size> bool reserve_helper(Container &container, Size size, std::true_type) { container.reserve(size); return true; }

Not sure what can of worms that would open though.

hmoffatt commented 1 year ago

Can you use Container::size_type?