Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark function argument as output

C# allows to mark function argument as output only:

void func(out int i)
{
    i = 44;
}

Is it possible to do something similar in C/C++? This could improve optimization. Additionally is should silence out warnings "error: 'myVar' may be used uninitialized in this function", when variable is not initialized and then passed to function as output argument.

I use gcc/g++ (currently 4.4.7) to compile my code.

Edit: I know about pointers and references, this is not what I am looking for. I need something like this:

void func(int* __attribute__((out)) i)
{
    *i = 44;
}

void func2()
{
    int myVal; // gcc will print warning: 'myVar' may be used uninitialized in this function
    func(&myVal);
    //...
}

Edit 2: Some extra code is needed to reproduce warning "'myVar' may be used uninitialized in this function". Additionally you have to pass -Wall -O1 to gcc.

void __attribute__((const)) func(int* i)
{
    *i = 44;
}

int func2()
{
    int myVal; // warning here
    func(&myVal);
    return myVal;
}
like image 230
Daniel Frużyński Avatar asked Jun 10 '15 09:06

Daniel Frużyński


People also ask

Can an output be a parameter?

Output parameters. An output parameter, also known as an out parameter or return parameter, is a parameter used for output, rather than the more usual use for input.

How do you pass a function as an argument?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.

What is an output parameter C++?

Out-parameters. An out-parameter represents information that is passed from the function back to its caller. The function accomplishes that by storing a value into that parameter. Use call by reference or call by pointer for an out-parameter.

What is the syntax for a function with arguments?

When one piece of code invokes or calls a function, it is done by the following syntax: variable = function_name ( args, ...); The function name must match exactly the name of the function in the function prototype. The args are a list of values (or variables containing values) that are "passed" into the function.


2 Answers

"Is it possible to do something similar in C/C++?"

Not really. Standard c++ or c doesn't support such thing like a output only parameter.

In c++ you can use a reference parameter to get in/out semantics:

void func(int& i) {
          // ^
    i = 44;
}

For c you need a pointer, to do the same:

void func(int* i) {
          // ^ 
    *i = 44;
 // ^
}

Note that there are no distinctions between out and in&out parameters, unless you use a const reference (which means input only):

void func(const int& i) {
       // ^^^^^
    i = 44;
}
like image 53
πάντα ῥεῖ Avatar answered Oct 04 '22 05:10

πάντα ῥεῖ


When you want to pass an arg as output in C++ you pass it as a reference :

 void func(int &i)
{
    i = 44;
}

and you must initialize it before doing anything on it.

Note : You can specify when you want the arg to be just an input with a const ref :

 void func(const int &i)
{
    i = 44;
}
like image 45
Bastien Avatar answered Oct 04 '22 06:10

Bastien