-1

I have some code with lambda expression and my problem is that I would like to return something only if "if" isn't nullptr. Other way I don't want to return anything. Is there a return that can be used in this code? Or maybe another way to not getting warning?


auto iter = std::stable_partition(object1->vector_.begin(), object1->vector_.end(), [](Class* x)  
{
    if (x->object2_ != nullptr)
    {

        return !x->object2->parameter_;
    }

});

It's working good, but this warning is annoying and I know that I should do something with this.

1 Answers1

3

The intent of std::stable_partition is:

Reorders the elements in the range [first, last) in such a way that all elements for which the predicate p returns true precede the elements for which predicate p returns false. Relative order of the elements is preserved.

When you use

return !x->object2->parameter_;

you want to put all the elements for which !x->object2->parameter_ is true to the left and all the elements for which !x->object2->parameter_ is false to the right.

Judging by that, I would say if x->object2_ is a nullptr, you should put them to the right. Hence, the default return needs to be

return false;

Hence,

auto iter = std::stable_partition(object1->vector_.begin(),
                                  object1->vector_.end(),
                                  [](Class* x) -> bool
{
    if (x->object2_ != nullptr)
    {
        return !x->object2->parameter_;
    }

    return false;
});

You can combine the body of the function to one line as:

auto iter = std::stable_partition(object1->vector_.begin(),
                                  object1->vector_.end(),
                                  [](Class* x) -> bool
{
    return (x->object2_ != nullptr) && (!x->object2->parameter_);
});
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @VidelIchinose, that is true. You'll have to: 1) Make sure that there is no such object in the vector, or 2) Add code to deal with them. – R Sahu Sep 12 '19 at 21:36
  • This is the comment I deleted, sorry. Ok, I see, but after that, I have std::for_each(iter, object1->vector_.end(), [](Class *x) {delete x->object2; }); So when I'm using return false; I have null object2s... I think – Videl Ichinose Sep 12 '19 at 21:38
  • Is a deleting null object very bad? Because actually it is working and there is no warning, except some redundancy – Videl Ichinose Sep 12 '19 at 21:40
  • @VidelIchinose when you say *deleting null object*, do you mean the C++ statement `delete obj;`? – R Sahu Sep 12 '19 at 21:44
  • yes, exactly. Because if obj=nullptr and then we do delete obj; nothing happen, am I right? – Videl Ichinose Sep 12 '19 at 21:46
  • @VidelIchinose, yes you may. See https://stackoverflow.com/questions/25329576/calling-delete-on-null-pointers-c03-vs-c11. – R Sahu Sep 12 '19 at 21:49
  • @VidelIchinose But do you want to delete all `object2_`'s or only those with no `parameter_`? In the latter case you could use `iter` instead of `end()` in your `for_each`. – Ted Lyngmo Sep 12 '19 at 21:57