I am writing a function which should accept const std::string &
, but also a special nullptr
value. Currently, I'm using const std::string *
. However, I feel that there should be a more C++-ish way to perform this task.
My strongest counterpoint is passing by value (the string won't be really long but 64kB is not something I want copied around). That also implies that I want no object like optional<T>
which would have a value field of type T
. Also, I don't want to use any external libraries. My project is not using Boost or GSL, just plain C++ (C++17 to be exact).
Is there a standard library class (miracles occur in namespace std
) or a widely accepted idiom for such situation?
If std::optional
supported references, it'd be the fancy "modern" way to do this.
But:
The age-old, tried-and-tested and clear-to-read solution is to take a pointer. This approach does absolutely everything that you need it to do.
Just do that, then move on to spend your precious time on more important matters!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With