views:

76

answers:

6

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?

A: 

Nope :)

As an aside, some would poo-poo you for doing that even if it were possible.

As another aside, C++ isn't known for its conciseness.

J Cooper
A: 

To my knowledge, no. I think this would make your code more "concise" in the same sense that removing comments and whitespace does. It'll make it look shorter, but it will be far less readable. I like the fact that each function declaration has to be very explicit and individual in C++(though I've never encountered a language that allowed you to declare multiple functions). So, in short, no.

Rafe Kettler
Technically declaring multiple functions with one expression works.
Georg Fritzsche
+4  A: 

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.

Jerry Coffin
+2  A: 

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.

Benoît
A: 

Why would you need that? Typing few extra characters doesn't make much difference. If you need it for more than a couple of methods, you should refactor the code, so you don't have to pass that many parameters around.

Having said that, in some cases the right solution might be to use variadic functions, so you can do:

int f(const int arg1, ...)
ya23
+1  A: 

If you're taking that many parameters of the same type, it's time to wake up and take a std::/std::tr1::/boost:: array<int, count> const&. That's what arrays are for.

DeadMG