-
Default function argument values
I've always put default function argument values in the header file. I see some code where the programmer did not specify a default value in the header, but did specify it in the body. Should the compiler (g++) have allowed this?
When other compilation units include this header (with no default arguments defined), can the compiler know about the default arguments in the body of the other compilation unit? (I wouldn't think so.)
-
I always have it in both places so its very clear to the reader no matter where one looks. However, this is not "proper" really, its belongs only in the header. I do not know that its illegal or not to have it in the other place, but thats where it belongs.
Supposedly its an error to have it in both places, due to a redefination problem, but I do not know if thats a compiler problem or if allowing it in both places is a compiler playing nice when it should not.
The best result I found with some google action was that it can legally go in either place and that is fine, going in both should create an error (but may not on some compilers?), and it should go in the header as that is where the user of the function will look for it by convention. I do not know if this is true or not, but a few people said similar.
-
> I see some code where the programmer did not specify a default value
> in the header, but did specify it in the body.
> Should the compiler (g++) have allowed this?
The compiler should certainly allow it; it does not violate any C++ rule. (ODR is not violated as the default value is defined just once). The type of the function does not include default values for arguments - the type is the same; it is just that the compiler can generate the code to evaluate and provide these values if they are not specified by the programmer at the point of call.
Even this is valid:
Code:
void foo( int, int, int=7 ) ;
int main()
{
foo( 55, 666 ) ; // compiler generates foo( 55, 666, 7 ) ;
void foo( int=1, int=2, int=3 ) ;
foo( 55, 666 ) ; // compiler generates foo( 55, 666, 3 ) ;
foo() ; // compiler generates foo( 1, 2, 3 ) ;
}
void foo( int, int=22, int ) {}
This would not be good style, though.
>When other compilation units include this header (with no default
> arguments defined), can the compiler know about the default
> arguments in the body of the other compilation unit?
No. As far as the compiler is concerned, there are no default values for these arguments in this particular translation unit.
-
Can you elaborate on this: "No. As far as the compiler is concerned, there are no default values for these arguments in this particular translation unit. "?
Example. My main has this:
Code:
#include <iostream>
using namespace std;
#include "test.h"
int main()
{
cout < "This is a test" << function() << endl;
return 1;
}
test.h has this:
Code:
#ifndef TEST_H_
#define TEST_H_
int function(int a);
#endif /* TEST_H_ */
And test.cpp has this:
Code:
int function(int a=3)
{
return a;
}
This, I think correctly generates a compile error because when main is compiled, the compiler knows nothing about the default parameters in test.cpp.
Basically, I think we've answered my question with the obvious: default parameters specified in one compilation unit has no effect on another compilation unit because the compiler doesn't see them.
-
> default parameters specified in one compilation unit has no effect
> on another compilation unit because the compiler doesn't see them.
Yup. Actually we can make it stronger - default parameters that are not visible will not be evaluated by the compiler. For example,
Code:
void foo( int, int = 5 ) ;
int main()
{
foo(22) ; // fine, calls foo( 22, 5 ) ; void foo( int, int = 5 ) is visible
void foo( int, int ) ; // hides void foo( int, int = 5 )
foo(22) ; // error; there is no default value for parameter 2.
}
-
That said, there are still restrictions wrt default argument values. Here's what the standard says:
For non-template functions, default arguments can be added in later declarations of a function in the same scope. Declarations in different scopes have completely distinct sets of default arguments. That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa. In a given function declaration, all parameters subsequent to a parameter with a default argument shall have default arguments supplied in this or previous declarations. A default argument shall not be redefined by a later declaration (not even to the same value). [Example:
Code:
void f(int, int);
void f(int, int = 7);
void h()
{
f(3); // OK, calls f(3, 7)
void f(int = 1, int); // error: does not use default
// from surrounding scope
}
void m()
{
void f(int, int); // has no defaults
f(4); // error: wrong number of arguments
void f(int, int = 5); // OK
f(4); // OK, calls f(4, 5);
void f(int, int = 5); // error: cannot redefine, even to
// same value
}
void n()
{
f(6); // OK, calls f(6, 7)
Danny Kalev
Similar Threads
-
Replies: 26
Last Post: 12-01-2012, 04:12 AM
-
Replies: 4
Last Post: 04-14-2006, 09:09 AM
-
By lucbrunet in forum VB Classic
Replies: 5
Last Post: 04-21-2005, 04:02 PM
-
By Patrick Comeau in forum VB Classic
Replies: 6
Last Post: 03-22-2001, 10:50 PM
-
By Bob Hines in forum Database
Replies: 7
Last Post: 04-27-2000, 11:14 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks