1

Given the code below. Are we correct to assume this is a FP detection and we can safely disable the warning or not? We assume it is a false positive as the result of std::to_string(int) will exists on the stack till the function call is over, but of course we want to be sure ...

// Definition of the function to be called
void my_function(const std::vector<std::string_view>& args) {}

// The function call complaining
my_function({
    std::to_string(10)    //error C26449: gsl::span or std::string_view created from a temporary will be invalid when the temporary is invalidated
});

// Of course we could write the code like this, but yeah we would like to know the theory and prefer the less bulk notation :)
auto var = std::to_string(10);
my_function({
   var
});

It all comes down to when will the std::string from std::to_string will be disposed.

Evg
  • 25,259
  • 5
  • 41
  • 83
  • 1
    Whether or not the warning should be considered a "false positive" is, largely, a matter of opinion. You are passing a temporary object *by reference* so, I guess it depends on what you expect your function to do with that reference. The compiler doesn't know, and neither do we. – Adrian Mole Jul 09 '21 at 07:57
  • As an aside, why not use a span of string views instead? – eerorika Jul 09 '21 at 08:19
  • Hi @AdrianMole, indeed the function below is aware that it gets a string_view and not sting. So we got that covered. I am not complaining about the analyzer ;-). We just want to be sure we aren't working with already disposed data and that we can safely ignore the warning. – user1202648 Jul 09 '21 at 08:21
  • The warning is just that ... a *warning*. If your function were to copy the reference it's given (to, say, a global class object's member), then you would be looking at a time-bomb. – Adrian Mole Jul 09 '21 at 08:26
  • @eerorika Not familiar with spans yet, we recently started using c++17 ... and not all new features are applied at once to all code. But thanks for the hint ;-)! – user1202648 Jul 09 '21 at 08:28
  • @AdrianMole Indeed, but the whole point of passing it by reference is to avoid copies ... same as for the existence of string_views. We are familiar with such coding and I am not gonna explain our application in depth, but we are one of those hitting the HW limits ... thus every copy counts... – user1202648 Jul 09 '21 at 08:32
  • I personally cannot generate that warning with clang 12.0.0. compiled code with `--std=c++17 -Wall -Wextra -pedantic`. – Afshin Jul 09 '21 at 12:24

1 Answers1

0

We wrote a small test program and it seems to work as expected. We will just disable the warning.

namespace std {
    class test_string : public string
    {
    public:
        test_string() : string()  { std::cout << "test_string()"; }
        ~test_string() { std::cout << "~test_string()"; }
    };
}

void test(const std::vector<std::string_view>& args)
{
    std::cout << "test()";
}

int main(int argc, char* argv[])
{
    std::cout << "begin_test";
    test({
      std::test_string()
    });
    std::cout << "end_test";
}
  • How to disable a particular warning? – Sourav Kannantha B Dec 28 '21 at 13:45
  • Here you can find some was of doing it: https://stackoverflow.com/questions/38060411/visual-studio-2015-wont-suppress-error-c4996 . It depends on what level you want to disable it, since it were specific pieces of code we wrapped them with pragma push/disable/pop ... – user1202648 Dec 28 '21 at 18:20
  • I tried to find a warning code for my problem, but was unsuccessful. Can you just checkout [this](https://stackoverflow.com/q/70508254/12357035) question I posted. – Sourav Kannantha B Dec 28 '21 at 20:00