2

I have to handle the [[nodiscard]] warning from std::remove;

static_cast<void>(std::remove(stringVar.begin(), stringVar.end(), ' '));

I want the correct way to do it. The warning can be stopped by below code:

auto temp = std::remove(stringVar.begin(), stringVar.end(), ' ');

I dont want to use the return value of std::remove.

void main()
{
    std::string stringVar { "Operation : In , Value : 3884 ," };

    size_t from = 0, to = 0, pos = 0;
    std::string delFrom{ ":" }, delTo{ "," };

    static_cast<void>(std::remove(stringVar.begin(), stringVar.end(), ' '));

    from = stringVar.find(delFrom, pos);
    to = stringVar.find(delTo, pos);
    std::cout<< stringVar.substr(from + 1, to - from - 1);
}

Output:

In

This is a specific question do not interested in already searched question on SO.

Update: Data consistent and readable format.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
  • 3
    The problem here is that if you don't use `temp` you haven't properly removed the spaces from your string. – john Nov 10 '20 at 06:04
  • 5
    If you're getting a warning about discarding a return value, that supposed to be interpreted as a sign that your code has problems, *not* that you should find a way to silence the warning. – Nicol Bolas Nov 10 '20 at 06:08
  • @john I am doing search again on stringVar for first occurrence delimiter. So, I am not interested in garbage pointed by return iterator – Build Succeeded Nov 10 '20 at 06:24
  • This is something like "the check engine light of my car is on, how do I remove the bulb?" – JHBonarius Nov 10 '20 at 06:30
  • What are you trying to archive? Remove is designed to work with erase. You should probably use another algorithm for what you want. We can help you find it. – JHBonarius Nov 10 '20 at 06:32
  • @JHBonarius example is updated – Build Succeeded Nov 10 '20 at 07:03
  • 1
    Note that with input such as `" "`, output is allowed to be "hello". `std::partition` seems more appropriate. – Jarod42 Nov 10 '20 at 08:44

2 Answers2

3

The problem here is that if you don't use temp you haven't properly removed the spaces from your string.

The correct code is

auto temp = std::remove(stringVar.begin(), stringVar.end(), ' ')
stringVar.erase(temp, stringVar.end());

You see std::remove does not remove anything from anything (how can it when all it has are two iterators?). All it does is rearrange the string so that the items at the end of the string are the part of the string that should be erased (you can think of it as moving all the spaces to the end of the string but actually it's more complicated than that).

To actually erase you need to call string::erase, using the iterator returned by std::remove as the code above shows.

john
  • 85,011
  • 4
  • 57
  • 81
  • I dont want to use erase-remove idiom. I am not interested in return value. – Build Succeeded Nov 10 '20 at 06:20
  • @BuildSucceeded To confirm, you want the "removed" elements to hang around at the end of the container? – user4581301 Nov 10 '20 at 06:24
  • 2
    @BuildSucceeded Using `std::remove` for a purpose other than removing items is such a strange thing that perhaps you should have made that clear in the question. Otherwise almost everyone is going to assume that you are just a beginner making a beginners mistake. So you are just interested in the rearrangement that `std::remove` performs? For what reason? – john Nov 10 '20 at 06:24
  • @user4581301 I am fine with those, as I am going for some search which does not go to those – Build Succeeded Nov 10 '20 at 06:25
  • sorry @john or confusing question, but I stressed on handling warning. I will improve question – Build Succeeded Nov 10 '20 at 06:26
  • Accepted the Answer as it is the correct way to keep the data and code in understandable format – Build Succeeded Nov 10 '20 at 09:09
0

As pointed out by @john, the operation won't work if you discard the value. That is the whole point of the [[nodiscard]] specifier which is to prevent the user from ignoring its value.


If you have a good reason to ignore the warning, the method you already suggest is the best way to do this. Which is to cast it to void.

static_cast < void >

If you wanna use a macro

#define ignore(x) (static_cast < void > (x))

Read more: How can I intentionally discard a no-discard return value?