Menu

FP "unassignedVariable" with C++ 17s structured binding

2021-08-04
2021-08-04
  • Dan Workman

    Dan Workman - 2021-08-04

    Found in version 2.5

    When using C++17s structured binding in range based for loops cppcheck is claiming that the first variable is never assigned:

    struct Pair
    {
      Pair(int first, int second) : first(first), second(second) {};
      int first;
      int second;
    };
    
    std::vector<Pair> GetPairs()
    {
      return {{0, 1}, {1, 2}};
    }
    
    int main()
    {
      for (const auto& [first, second] : GetPairs())
      {
        std::cout << first << second;
      }
    }
    

    generates:

    cppcheckWarnings.cpp:35:21: warning: Variable 'first' is not assigned a value. [unassignedVariable]
      for (const auto& [first, second] : GetPairs())
                        ^
    

    Dropping the reference fixes the issue but in our actual use case we are passing out custom iterators

     

    Last edit: Dan Workman 2021-08-04
  • CHR

    CHR - 2021-08-04

    Sounds similar to this ticket: https://trac.cppcheck.net/ticket/10368

     
    👍
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.