An open API service indexing awesome lists of open source software.

https://github.com/reputeless/namedparameter

Simple named parameter interface for modern C++
https://github.com/reputeless/namedparameter

cpp namedparameter

Last synced: 2 months ago
JSON representation

Simple named parameter interface for modern C++

Awesome Lists containing this project

README

        

# siv::NamedParameter
**siv::NamedParameter** is a simple named parameter interface for modern C++.

## Examples
This prints `month: 12 day: 31` to stdout:
```cpp
namespace Arg
{
SIV_MAKE_NAMED_PARAMETER(month);
SIV_MAKE_NAMED_PARAMETER(day);
}

void Date_impl(int month, int day)
{
std::cout << "month: " << month << " day: " << day << '\n';
}

void Date(Arg::month_ m, Arg::day_ d)
{
Date_impl(m.value(), d.value());
}

void Date(Arg::day_ d, Arg::month_ m)
{
Date_impl(m.value(), d.value());
}

int main()
{
Date(Arg::month = 12, Arg::day = 31);
Date(Arg::day = 31, Arg::month = 12);
}
```

Arguments can also be accessed by `operator*` and `operator->`.
```cpp
namespace Arg
{
SIV_MAKE_NAMED_PARAMETER(name);
}

void Print(Arg::name_ name)
{
std::cout << "name:" << *name << '\n';
std::cout << "length:" << name->length() << '\n';
}

int main()
{
Print(Arg::name = "John");
}
```

`operator()` can construct the object from the arguments to the constructor of the element.
```cpp
namespace Arg
{
SIV_MAKE_NAMED_PARAMETER(name);
}

void Print(Arg::name_ name)
{
std::cout << "name:" << *name << '\n';
std::cout << "length:" << name->length() << '\n';
}

int main()
{
Print(Arg::name(5, 'A'));
}
```

Right-most parameters can have default arguments.
```cpp
namespace Arg
{
SIV_MAKE_NAMED_PARAMETER(name);
}

void Print(Arg::name_ name = (Arg::name = "anonymous"))
{
std::cout << "name:" << *name << '\n';
std::cout << "length:" << name->length() << '\n';
}

int main()
{
Print();
}
```

## License
NamedParameter is distributed under the MIT license.