Search code examples
c++identifier

Mettle C++ Test Framework's use of undescore


Looking at the mettle test framework they have code like this:

#include <mettle.hpp>
using namespace mettle;

suite<> basic("a basic suite", [](auto &_) {

  _.test("a test", []() {
    expect(true, equal_to(true));
  });

  for(int i = 0; i < 4; i++) {
    _.test("test number " + std::to_string(i), [i]() {
      expect(i % 2, less(2));
    });
  }

  subsuite<>(_, "a subsuite", [](auto &_) {
    _.test("a sub-test", []() {
      expect(true, equal_to(true));
    });
  });

});

Is there something special going on with the use of underscore or is it a valid variable name?


Solution

  • Is there something special going on with the use of underscore

    Underscore is a valid character to use in an identifier. Some uses of underscore in an identifier are reserved for the implementation: What are the rules about using an underscore in a C++ identifier? But none of those apply to a single underscore in block scope.

    is it a valid variable name?

    Here, yes. It wouldn't be in global namespace.