rosidl icon indicating copy to clipboard operation
rosidl copied to clipboard

MSVC warnings when using std::fill in generated messages with types smaller than int

Open jacobperron opened this issue 6 years ago • 0 comments

Bug report

Follow up from ros2/rcl_interfaces#42.

Required Info:

  • Operating System:
    • Windows 10
  • Installation type:
    • from source
  • Version or commit hash:
    • 1653fa254400ef1b364842b7d24c9db4032bd57a (master)
  • DDS implementation:
    • Fast-RTPS and RTI Connext

Steps to reproduce issue

Either revert the patch introduced by #307 and build test_msgs, or for a SSCCE:

  1. Copy the following program into Microsoft Visual Studio:
#include <vector>
int main(void)
{
	std::vector<char> vec(10);
	std::fill(vec.begin(), vec.end(), 0);
	return 0;
}
  1. Turn on warning level W4 or higher:

Project -> Properties -> C/C++ -> Warning Level

  1. Build

Expected behavior

Similar to g++, we would expect the program to compile fine without warnings.

Actual behavior

Compiler warnings:

>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(2888): warning C4244: '=': conversion from 'const int' to 'char', possible loss of data
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(2902): note: see reference to function template instantiation 'void std::_Fill_unchecked1<_FwdIt,_Ty>(_FwdIt,_FwdIt,const _Ty &,std::false_type)' being compiled
1>        with
1>        [
1>            _FwdIt=char *,
1>            _Ty=int
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\xutility(2910): note: see reference to function template instantiation 'void std::_Fill_unchecked<char*,_Ty>(_FwdIt,_FwdIt,const _Ty &)' being compiled
1>        with
1>        [
1>            _Ty=int,
1>            _FwdIt=char *
1>        ]
1>c:\users\osrf\source\repos\project1\project1\main.cpp(5): note: see reference to function template instantiation 'void std::fill<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,int>(_FwdIt,_FwdIt,const int &)' being compiled
1>        with
1>        [
1>            _Ty=char,
1>            _FwdIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<char>>>
1>        ]

Additional information

See TODOs in the rosidl_generator_cpp with a workaround:

https://github.com/ros2/rosidl/blob/6c8e2deaa22e7be78b827820d3471be046a536a4/rosidl_generator_cpp/resource/msg__struct.hpp.em#L103-L104

https://github.com/ros2/rosidl/blob/6c8e2deaa22e7be78b827820d3471be046a536a4/rosidl_generator_cpp/resource/msg__struct.hpp.em#L124-L125

See ros2/rcl_interfaces#42 for more information.

jacobperron avatar Oct 24 '18 22:10 jacobperron