2

I am reading the following C++17 code:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <fstream>
#include <tuple>
using namespace std;

enum class Relationship { parent, child, sibling };
struct Person { string name; };
struct Relationships
{
    vector<tuple<Person, Relationship, Person>> relations;
    void add_parent_and_child(const Person& parent, const Person& child)
    {
        relations.push_back({parent, Relationship::parent, child});
        relations.push_back({child, Relationship::child, parent});
    }
};

struct Research
{
    Research (Relationships& relationships)
    {
        auto& relations = relationships.relations;
        for(auto&& [first, rel, second] : relations)
        {
            if(first.name == "John" && rel == Relationship::parent)
            {
                cout << "John has a child called "  << second.name << endl;
            }
        }
    }
};

int main()
{
    Person parent {"John"};
    Person child1 {"Chris"}, child2 {"Matt"};
    Relationships relationships;
    relationships.add_parent_and_child(parent, child1);
    relationships.add_parent_and_child(parent, child2);

    Research _(relationships);

    return 0;
}

That code compiles and works fine. What I do not get is the line:

    Research _(relationships);

Why is there an underscore? Is this a new feature of C++17? What is it called?

halfer
  • 19,824
  • 17
  • 99
  • 186
J. Doe
  • 441
  • 1
  • 4
  • 13

1 Answers1

0

Wow, it turns out to be a variable name, valid in C++17. That's a very lazy way of naming things.

halfer
  • 19,824
  • 17
  • 99
  • 186
J. Doe
  • 441
  • 1
  • 4
  • 13
  • It’s not necessarily lazy—sometimes it means that the name doesn’t matter because the variable exists for the effects of creating/destroying it. See the duplicate for yet other possible reasons. – Davis Herring Mar 24 '20 at 20:38