locked
Compiler Warning C26486 ranged for-loop RRS feed

  • Question

  • So, I'm just not getting the whole lifetime thing with this code:

    using D_String = Multi_String<String_Type::dictionary>;

    int main()

    { std::vector<D_String> dictionary_vector = { "cat","CAT","DOG","dog", "fish","Dog","" }; std::cout << "Before sort : "; for (const auto& s : dictionary_vector) std::cout << s << " "; // Highlights cout << s

    For What its worth, The constructor for Multi_String looks like this:

    template <String_Type ST>
    class Multi_String
    {
    	std::string s;
    public:
    	Multi_String(const char* cstr) : s{ cstr } {}
    	Multi_String(const std::string& s_) : s{ s_ } {}
    	Multi_String(std::string&& s_) noexcept : s{ std::move(s_) } {}
    	auto operator<=>(const Multi_String<ST>& rhs) const;
    	bool operator==(const Multi_String<ST>& rhs) noexcept;
    	const char* c_str() const noexcept;
    
    	friend std::ostream& operator<< (std::ostream& out, const Multi_String<ST>& s);
    };
    
    

    Any clue as to how this warning is helping the user (me)? Are there things that I should be actually doing about this code to make the error go away (other than #pragma warning(disable:26486)?

    Thanks!

    Sunday, December 1, 2019 5:35 AM

All replies

  • 1)

    ST in Multi_String seems to have no purpose. Anyway the template definition is wrong, the right one is:

    template <class ST>
    class Multi_String

    2)

    Inside the class,  operator <=> is undefined, perhaps you mean !=

    3)

    operator<< uses a template, the right definition is:

    template <class ST>
    friend std::ostream& operator<< (std::ostream& out, const Multi_String<ST>& s);

    4)

    D_String is, for example:

    using D_String = Multi_String<std::string>;

    With these corrections and the implementation of operator<< the code compiles and runs.





    • Edited by BlueLed Sunday, December 1, 2019 1:09 PM
    Sunday, December 1, 2019 1:07 PM
  • Unfortunately you are missing a couple of pieces of the puzzle here and it is not possible to reproduce using the code you provided. There is also no real scope issues in the code that you provided so it must be in something that you didn't provide.

    But anyway, all warnings tell you that there is potentially something wrong with your code that could cause unexpected behaviour. In this case it is telling you that after analysing your code (5 digit warning numbers come from code analysis) it sees that you have a potentially dangling pointer. This really should be a big concern because dangling pointers mean that you are not handling the lifetime of your objects correctly and can either cause crashes or security problems.

    So for people to fully help it would be useful to get a sample that actually compiles and shows this code analysis warning. This would mean providing the body of Multi_String::operator<< what you are using for String_Type::dictionary and if it is a concept like your code implies, the definition of the String_Type concept.

    If it is not a concept then syntactically correct code is also useful.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Edited by Darran Rowe Sunday, December 1, 2019 2:48 PM
    Sunday, December 1, 2019 2:47 PM
  • What does the implementation of operator<< look like?

    Igor Tandetnik

    Sunday, December 1, 2019 3:49 PM
  • In case the op meant to use a Non-type template parameter like int ST, for example, the code still seems to compile.

    • Edited by BlueLed Sunday, December 1, 2019 4:11 PM
    Sunday, December 1, 2019 4:11 PM
  • I guess, some code related to Multi_String and operator<< can be found here:

    https://codereview.stackexchange.com/questions/233129/playing-with-operator-operator-spaceship-in-c/233133#233133

    Where i also learn operator <=> really exists:

    https://en.cppreference.com/w/cpp/language/operator_comparison

    Edit:

    At first glance, at some point in the code in the link, i see things like this:

    return s.c_str();
    this might cause the dangling pointer issue.
    • Edited by BlueLed Sunday, December 1, 2019 6:15 PM
    Sunday, December 1, 2019 6:09 PM
  • Hmm, that link is actually useful and it actually got the code analysis warning.

    But:

    C:\Users\Darran\source\repos\meh\meh\main.cpp(147): warning C26486: Don't pass a pointer that may be invalid to a function. Parameter 1 '@dictionary_vector' in call to 'operator<<' may be invalid (lifetime.3).

    So it is complaining about the temporary variable that the ranged for creates for the body of the loop.

    But this is actually one of the problems with static code analysis. Sometimes there are code analysis warnings or errors that you as a programmer can see that it isn't a problem. But this is why the code analysis option defaults to the recommended ruleset and not all rules. This is the same as how the compiler defaults to /W3 and not /Wall since /Wall produces a lot of warnings that are easily ignorable, like truncation of variables and the like.

    I would honestly tell the OP to switch the code analysis settings to the Microsoft Native Recommended Rules.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Sunday, December 1, 2019 10:31 PM