Reputation: 396
I'm pretty new to programming and can't really understand why I can't just declare argument types the same way I do with normal variables and have to declare the type again and again.
I mean, why must I:
Func(int a, int b, float c)
instead of
Func(int a, b, float c)
?
As long as they're the same type, of course.
If it is possible, please tell me how.
Thanks in advance.
@0x499602D2: If parameter declarations were more closely analagous to object declarations, then void f(int a, float c, d) would presumably be equivalent to void f(int a, float c, float d). The language could have made this work correctly and consistently. It just didn't. – Keith Thompson
This answered my question best. but it's a comment...
Upvotes: 10
Views: 11100
Reputation: 44248
If you want b to have the same type as a (in case you later change type of a b should reflect that) in c++11
you can do this:
void Func( int a, decltype(a) b );
If you want to omit type at all, you cannot do that. Simple answer is: syntax does not support that. Why? There could be many answers, one of them:
void func( int, int ); // function declaration
what would we put there now?
void func( int, ); // function declaration? ugly and unreadable
Also it is error prone:
void func( int a, foobar ); // suppose this works
// now we add #include where foobar is defined as a struct
// function suddenly changes it's signature
I am pretty sure there would be even more bad side effects, so just believe, you do not want that.
Upvotes: 2
Reputation: 153830
I realize that this isn't quite answering the question probably asked but it literally answers the asked question: Yes, it is possible to declare a function, say f
, which takes N
arguments of type T
(and returns RC
) without repeating T
although using a funny notation:
same_type_function<void, 2, int> f;
Something similar can be done for member functions. Note, however, that I don't think that is also possible to define functions like this! Also, you'll obviously need a definition of same_type_function
. Below is a quick example how this template can be implemented:
#include <iostream>
template <typename RC, int N, typename T, typename... A>
struct same_type_function_aux;
template <typename RC, typename T, typename... A>
struct same_type_function_aux<RC, 0, T, A...> {
typedef RC (type)(A...);
};
template <typename RC, int N, typename T, typename... A>
struct same_type_function_aux {
typedef typename same_type_function_aux<RC, N-1, T, T, A...>::type type;
};
template <typename RC, int N, typename T>
using same_type_function = typename same_type_function_aux<RC, N, T>::type;
same_type_function<void, 2, int> f;
int main()
{
f(1, 2);
}
void f(int a, int b) {
std::cout << "called f(" << a << ", " << b << ")\n";
}
Upvotes: 0
Reputation: 7257
This is why:
Everything has some rules or works on contracts. In theory you could write a C compiler that will instead of:
func(int a, int b)
take this:
func(int a, b)
that would be perfectly fine.
but
Creators of C decided that every single formal argument has to have its type attached to it hence we have it today. It's just a convention which you must follow.
And you must follow it as C/C++ parser is expecting you to do it this way otherwise it will not understand you.
Similarly your question:
Is there a way to declare multiple function arguments with one type?
may theoretically be written this way:
there multiple a way Is to declare function arguments with one type?
If you agree with someone to construct questions this way you must follow this contract - period.
Upvotes: 7
Reputation: 5203
That's how the syntax is. I don't know of there is a "reason", but in (old) C arguments without an explicit types would default to int (and there was even a syntax to provide types after the closing parenthesis), so I'm not sure this could be relaxed safely.
Upvotes: 3
Reputation: 5612
No, i don't think so.
In your function declarations, however, you can leave out the variable names.
Func(int a, int b);
can be
Func(int, int);
The compiler just needs enough information about the signature in order to be able to determine which function to call at runtime.
Upvotes: 1