13

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;
}
Daniel Frużyński
  • 2,091
  • 19
  • 28
  • Do you want pointers? Like `void func(int* i){*i = 44;}` (C)? – Spikatrix Jun 10 '15 at 09:23
  • 1
    C and C++ are different languages. In C++ you should use references, in C you only have pointers. – user4520 Jun 10 '15 at 09:23
  • I need additional attribute to mark ptr/ref argument as out only. I updated my question to state this. – Daniel Frużyński Jun 10 '15 at 09:35
  • My g++ [does not warn anything](http://coliru.stacked-crooked.com/a/e813548cc65d4546) even with `-Wall -Wextra -pedantic` flags and even if `myVal` is used in `func2`. I'm assuming that you meant to pass the address of `myVal` into `func`. – eerorika Jun 10 '15 at 09:51
  • Please see my edit 2, some extra code plus -O1 was needed. – Daniel Frużyński Jun 10 '15 at 10:03
  • Just out of pure curiosity, why do you need this ? – undu Jun 10 '15 at 10:06
  • Oh, and why all the downvotes ? – undu Jun 10 '15 at 10:06
  • 1
    Having lied to the compiler with `__attribute__((const))`, which promises that the function ["does not examine any values except its arguments, and has no effects except the return value"](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html), you now complain that the compiler is fooled by the lie? – T.C. Jun 10 '15 at 10:07
  • I think pointers are the only option for this in C/C++. – Jyothish Bhaskaran Nov 19 '21 at 18:34

2 Answers2

13

"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;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Note that there are no distinctions between `out` and `in&out` parameters. – Jarod42 Jun 10 '15 at 09:24
  • Unless you use `const` as explained in another answer ;) – user4520 Jun 10 '15 at 09:28
  • I need additional attribute to mark ptr/ref argument as out only. I updated my question to state this. – Daniel Frużyński Jun 10 '15 at 09:35
  • @DanielFrużyński _"I need additional attribute to mark ptr/ref argument as out only."_ You simply cannot in standard c++ or c. – πάντα ῥεῖ Jun 10 '15 at 09:41
  • I know this, and I am looking for some non-standard, probably gcc-only extension. – Daniel Frużyński Jun 10 '15 at 09:48
  • @DanielFrużyński _"I am looking for some non-standard, probably gcc-only extension"_ Not that I'm aware of one. I'd say that the semantics as defined in the language standards for c++ or c, inherently won't allow such extension being implemented. – πάντα ῥεῖ Jun 10 '15 at 09:49
  • gcc already implements additional attributes for functions, variables and types. Attribute for function argument could look like this: `void func(int* __attribute__((out)) i)` or `void func(__attribute__((out)) int* i)` – Daniel Frużyński Jun 10 '15 at 10:05
  • it's such a pity C++ doesn't have an `out` designation, because sometimes that it the pattern and it would be super nice if the compile gave you similar warning as what you get in C# eg when you forget to set the value. – orion elenzil Nov 19 '21 at 18:31
2

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;
}
orion elenzil
  • 4,484
  • 3
  • 37
  • 49
Bastien
  • 994
  • 11
  • 25