Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Functions With std::optional Parameters

I have a function whose signature is:

void func(std::optional<std::string> os = std::nullopt);

(I’m aliasing std::experimental::optional until std::optional is officially available.)

However, I’m having difficulty calling it cleanly. The compiler will refuse to perform two implicit conversions (const char*std::stringstd::optional<std::string>) to call it with a raw C-string literal. I can do this:

func(std::string("Hello"));

And the compiler will figure that a std::optional is needed, and do the conversion. However, this is way too verbose. Thanks to C++11, I can also do this:

func({"Hello"});

While this is way better, it's still not ideal. I'd like to be able to call this function like any other that takes a std::string. Is this possible? Making the function take another parameter type is okay, as long as it behaves similarly to/is directly convertible to std::optional. Thanks.

like image 994
ThatsJustCheesy Avatar asked Feb 06 '17 00:02

ThatsJustCheesy


People also ask

How do you pass an optional parameter to a function?

To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.

Can you have optional parameters in C++?

In C++, optional parameters are among the most important concepts to create complex programs. There are the values which are assigned to a function by default. We use optional parameters when we want a user to override its value in a function.

Are function parameters optional?

The function parameters can also be optional, which gives us the independence to pass function arguments when we call the function. Here, Arguments are the values we pass while calling the function, and parameters are the variables we pass in the function definition.

What is the use of std :: optional?

std::optional contains the object within itself, depending on where it is stored (stack/data/heap) std::optional makes a copy of the contained object. Monadic functions will be added in C++23 to improve the abstraction in our code by removing the needs of writing boilerplate code.


1 Answers

C++14 adds a bunch of user-defined literals to the standard library in order to make code less verbose. It looks something like this:

using namespace std::string_literals;              // needed
// using namespace std::literals;                  // also ok, but unnecessary 
// using namespace std::literals::string_literals; // also ok, but why??

int main()
{
    std::string str = "string"s;
                       ^^^^^^^^
                       // This is a std::string literal, 
                       // so std::string's copy constructor is called in this case
}

Also take a look at this and this for reference.

like image 155
DeiDei Avatar answered Oct 20 '22 15:10

DeiDei