myPreciousFunction(std::string s1 = "", std::string s2 = "")
{
}
int main()
{
myPreciousFunction();
}
Can i make the arguments look more pretty? I want there to be empty strings if no arguments were supplied.
Function parameters are not allowed in default arguments except if they are unevaluated. Note that parameters that appear earlier in the parameter list are in scope : Operator functions shall not have default arguments, except for the function call operator:
For non-template functions, default arguments can be added to a function that was already declared if the function is redeclared in the same scope. At the point of a function call, the defaults are a union of the defaults provided in all visible declarations for the function.
A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value. Following is a simple C++ example to demonstrate use of default arguments.
Default arguments in Python. Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.
you may consider this:
myPreciousFunction(std::string s1 = std::string(), std::string s2 = std::string())
{
}
But it doesn't really look prettier.
Also, if you're passing strings, you might want to pass them as const&
:
myPreciousFunction(const std::string& s1, const std::string& s2)
{
}
This is a standard way to avoid coping the data around.
There is actually another solution.
const std::string empty = std::string();
myPreciousFunction( const std::string &s1 = empty, const std::string &s2 = empty)
This has the advantage of avoiding construction of temporary objects.
You could use a braced initialisation:
myPreciousFunction(const std::string& s1 = {}, const std::string& s2 = {})
{
}
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