[libcamera-devel] [PATCH v2 2/4] v4l2: Replace manual loop counters with utils::enumerate()

Kieran Bingham kieran.bingham at ideasonboard.com
Tue May 18 12:43:28 CEST 2021


Hi Laurent,

On 18/05/2021 11:42, Laurent Pinchart wrote:
> Hi Kieran,
> 
> On Mon, May 17, 2021 at 07:36:43PM +0300, Laurent Pinchart wrote:
>> On Mon, May 17, 2021 at 01:54:08PM +0100, Kieran Bingham wrote:
>>> On 15/05/2021 05:05, Laurent Pinchart wrote:
>>>> Use the newly introduced utils::enumerate() to replace manual loop
>>>> counters. A local variable is needed, as utils::enumerate() requires an
>>>> lvalue reference.
>>>
>>> So this is not possible?
>>>
>>> 	for (auto [index, camera] : utils::enumerate(cm_->cameras()) {
>>
>> Unfortunately not :-(
>>
>>> I guess that's fine, but will it be obvious to someone trying to use it
>>> that they need to move the thing to enumerate to a local variable if
>>> it's not there?
>>
>> The compiler should make it obvious:
>>
>> ../../src/v4l2/v4l2_compat_manager.cpp: In member function ‘int V4L2CompatManager::start()’:
>> ../../src/v4l2/v4l2_compat_manager.cpp:85:53: error: no matching function for call to ‘enumerate(std::vector<std::shared_ptr<libcamera::Camera> >)’
>>    85 |         for (auto [index, camera] : utils::enumerate(cm_->cameras())) {
>>
>> utils::enumerate() takes an lvalue reference, and cm_->cameras() is an
>> rvalue.
> 
> I'll add the following to the documentation of the function:
> 
>  * Note that the argument to enumerate() has to be an lvalue, as the lifetime
>  * of any rvalue would not be extended to the whole for loop. The compiler will
>  * complain if an rvalue is passed to the function, in which case it should be
>  * stored in a local variable before the loop.

Perfect.

-- 
Regards
--
Kieran


More information about the libcamera-devel mailing list