Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound argument declaration

To make my code more concise, can I do something like

int f(int const x, y, z), g(int const x, y, z);

to declare functions f and g which each take three int const arguments?

Edit: Perhaps here is a better example:

int f(int const a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z);

How would that not be more concise and readable than putting int const before every letter?

like image 395
user383352 Avatar asked Aug 31 '26 07:08

user383352


2 Answers

For C code, if you really insist on it, you can use old-style (K&R) function headers:

typedef int const cint;

int f(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
 cint a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
{
    // function body here
}

Note that I'm not recommending this -- and C++ doesn't support this anyway, so the only way you could use it would be to compile the function itself in C, and include an extern "C" declaration to access it from C++.

I'd also note, however, that the whole idea strikes me as silly anyway. First, a function that has enough parameters for this to be worth considering is basically guaranteed to be a disaster. Second, top-level const (i.e., applying to the parameter itself, not what it points at or refers to) is completely meaningless, and (IMO) a lousy idea in any case.

like image 115
Jerry Coffin Avatar answered Sep 02 '26 06:09

Jerry Coffin


Using Boost preprocessor, and considering you don't care to describe the function arguments one by one (but isn't that the point of your question), you can use the following trick :

#include <boost/preprocessor/repetition/enum_params.hpp>

int f( BOOST_PP_ENUM_PARAMS(26, int const arg) );

But don't forget that the whole idea of not taking the required time and space to describe function arguments is very dangerous.

like image 29
Benoît Avatar answered Sep 02 '26 05:09

Benoît