3

I want to call a function that constructs a stringstream object and if some condition is met, invalidate the stringstream. When I try this, I get a warning from using a moved from object. How can I prevent this without switching to a string?

#include <iostream>
#include <sstream>
#include <functional>

using namespace std;

bool Condition(string) { return true; }

stringstream someFunc(function<bool(string&)> Condition) {
    stringstream ssRes; // Warning C26800 Use of a moved from object:'ssRes'

    ssRes << "here is a string";

    string str = ssRes.str();
    if (!Condition(str)) { ssRes.setstate(ios_base::failbit); }
    return ssRes;
}

int main() {
    stringstream ss = someFunc(Condition);
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
cainsr2
  • 55
  • 4
  • 5
    Can we get a [mre] so we can try and compile the code ourselves? – NathanOliver Mar 22 '22 at 14:32
  • What is the return type of RunScript? – Joseph Larson Mar 22 '22 at 14:34
  • Please don't post `...`. All that will do is have us fill in code that magically works and is warning-free, while your real code fails to compile. Then the comment section gets filled with "can't duplicate" comments. All compiler errors can be duplicated using dummy code that actually compiles. For example, do we really need to see `setstate`? – PaulMcKenzie Mar 22 '22 at 14:34
  • What compiler and compiling settings are you using? [I cannot reproduce](https://godbolt.org/z/GEnbe81dc) – NathanOliver Mar 22 '22 at 15:01
  • @NathanOliver this is a [code analysis warning](https://learn.microsoft.com/et-ee/cpp/code-quality/c26800?view=msvc-170&viewFallbackFrom=vs-2019) it's not from the compiler – Alan Birtles Mar 22 '22 at 15:06
  • Bad style to reuse the name `Condition` for both a function (in global scope) and a function-object parameter (in `someFunc` scope) – Ben Voigt Mar 22 '22 at 15:08
  • Looks like a false positive then. `ssRes` is not moved from until the function ends, so it is fine. – NathanOliver Mar 22 '22 at 15:09
  • 3
    A [mre] would seem to be https://godbolt.org/z/cPxYd9Gb9 and should be reported to Microsoft as a bug – Alan Birtles Mar 22 '22 at 15:25
  • Sorry for the bad example, I will make sure to use minimal reproducible examples in the future. Yes, I am using Microsoft's compiler. Suspected it might be a bug but was not sure. Thank you. – cainsr2 Mar 22 '22 at 17:15
  • The cause of the warning should be a false positive. According to this [issue](https://developercommunity.visualstudio.com/t/code-analysis-false-warning-c26800/1497147), the false positive has been fixed, I suggest you to download VS in the link to test. – Yujian Yao - MSFT Mar 23 '22 at 06:21

1 Answers1

0

This is a bug that has been fixed in Visual Studio 2022 version 17.3.

If updating VS isn't an option, you can temporarily disable this warning with #pragma warning(suppress: 26800).

#include <iostream>
#include <sstream>
#include <functional>

using namespace std;

bool Condition(string) { return true; }

stringstream someFunc(function<bool(string&)> Condition) {
    stringstream ssRes; // Warning C26800 Use of a moved from object:'ssRes'

    ssRes << "here is a string";

    string str = ssRes.str();
    if (!Condition(str)) { ssRes.setstate(ios_base::failbit); }
#pragma warning(suppress: 26800)
    return ssRes;
}

int main() {
    stringstream ss = someFunc(Condition);
    return 0;
}
Jacob Bischoff
  • 126
  • 1
  • 12