-2

Can we write function in c++, something like this, may be not exactly like this, syntax may be different

void FunctionTest(int param1, _Inout_ int& param2 = 10)
{
    param2 = 20;
}

int main() {
    int a = 100, int b = 200;
    FunctionTest(10);
    FunctionTest(a, b);
}

Not necessary it should be pass by reference, may other alternative will also work pointer or any other way.

4
  • 3
    Is it possible in C++ passing function inout parameter with default value? No. Commented Jul 2, 2024 at 18:20
  • Your compiler should immediately reject this. Commented Jul 2, 2024 at 18:22
  • 3
    Compiler quite reasonable says "error: non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'" to your snippet. OTOH, int foo = 10; void FunctionTest(int param1, int& param2 = foo) . . . is just fine. ` Commented Jul 2, 2024 at 18:23
  • Inout is Microsoft annotation, it would be better if you just forget about their existence Commented Jul 2, 2024 at 18:24

2 Answers 2

5

Yes, you can actually do it quite concisely with a helper function:

template <class T>
T &as_lvalue(T &&o) { return o; }

void FunctionTest(int param1, int& param2 = as_lvalue(10))
{
    param2 = 20;
}

Do keep in mind that the referenced object will only live until the end of the full-expression containing the call to FunctionTest. This is what makes non-const-reference parameters with default values quite an odd sight: why provide an output parameter if you won't be able to examine the results before it vanishes?

If you want to make a function that may provide additional output besides its return value, maybe a cleaner pattern would be a (natively nullable) pointer parameter and a separate overload for the reference:

void FunctionTest(int param1, int *param2 = nullptr)
{
    if(param2)
        *param2 = 20;
}

void FunctionTest(int param1, int &param2)
{
    return FunctionTest(param1, &param2);
}
Sign up to request clarification or add additional context in comments.

Comments

1

It is unclear why you think this would be needed. What would be the "out" when the default is used?

You can overload functions:

void FunctionTest(int param1,int& param2) {
    param2 = 20;
}

void FunctionTest(int param1) {}

int main() {
    int a = 100;
    int b = 200;
    FunctionTest(10);
    FunctionTest(a, b);
}

However, I cannot imagine this to be a good use case for overloading, because the two calls do something very different.


Only for the sake of completeness. A default arguemnt for reference can be a global:

   int foo;
   void function(int& b = foo);

Or a function local static or static class member:

   struct moo { static int x; }
   void function(int& b = moo::x);

   int& bar() { 
         static int x = 0;
         return x;
   }
   void function(int& b = bar::x);

Or more generally a function that returns a (valid) reference to any of the above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.