serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel
https://serizba.github.io/cppflow/
MIT License
779 stars 177 forks source link

Fix compiler type warnings #180

Closed gth828r closed 2 years ago

gth828r commented 2 years ago

This change fixes compiler warnings about comparing signed and unsigned types. To see these warnings when compiling with g++, you can use the -Wall flag. Note that in projects which also make all warnings errors (i.e. the -Werror flag in g++), these changes are necessary for programs that use cppflow to compile at all.

This addresses issue #142

gth828r commented 2 years ago

To test this out, I used the example listed in the quickstart page: https://serizba.github.io/cppflow/quickstart.html

When compiling that example program without any changes, I see the following warnings:

$ g++ -std=c++17 -o main.out -I /usr/local/include/ -I/home/vagrant/cppflow/include/ main.cpp -ltensorflow -Wall
In file included from /home/vagrant/cppflow/include/cppflow/cppflow.h:9,
                 from main.cpp:2:
/home/vagrant/cppflow/include/cppflow/model.h: In function ‘std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> cppflow::parse_name(const string&)’:
/home/vagrant/cppflow/include/cppflow/model.h:146:21: warning: comparison of integer expressions of different signedness: ‘long unsigned int’ and ‘int’ [-Wsign-compare]
  146 |         return (idx == -1 ? std::make_tuple(name, 0) : std::make_tuple(name.substr(0, idx), std::stoi(name.substr(idx + 1))));
      |                 ~~~~^~~~~
/home/vagrant/cppflow/include/cppflow/model.h: In member function ‘std::vector<cppflow::tensor> cppflow::model::operator()(std::vector<std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, cppflow::tensor> >, std::vector<std::__cxx11::basic_string<char> >)’:
/home/vagrant/cppflow/include/cppflow/model.h:154:24: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, cppflow::tensor> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  154 |         for (int i=0; i<inputs.size(); i++) {
      |                       ~^~~~~~~~~~~~~~
/home/vagrant/cppflow/include/cppflow/model.h:170:24: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::__cxx11::basic_string<char> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  170 |         for (int i=0; i<outputs.size(); i++) {
      |                       ~^~~~~~~~~~~~~~~
/home/vagrant/cppflow/include/cppflow/model.h:189:24: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::__cxx11::basic_string<char> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
  189 |         for (int i=0; i<outputs.size(); i++) {
      |      

With my changes, the program compiles without these warnings and it still runs.

serizba commented 2 years ago

Thanks, this looks nice!