valid function prototypes?

I was asked by a friend about validity of following function prototypes,

void func1(int = 0, int*);
void func2(int = 1, int& = 2);
void func3(int*, int& = 3);
void func4(int& = 4, int* = 0);
void func5(int& = 0, int = 1);
void func6(int = 5, int& = 6, int* = 0);

I think the only prototype that is invalid is func1 because it does not have default parameter on the far right.
I am not sure if I am right, though.
andrenvq57 wrote:
I was asked by a friend about validity of following function prototypes,
1
2
3
4
5
6
void func1(int = 0, int*);
void func2(int = 1, int& = 2);
void func3(int*, int& = 3);
void func4(int& = 4, int* = 0);
void func5(int& = 0, int = 1);
void func6(int = 5, int& = 6, int* = 0);

I think the only prototype that is invalid is func1 because it does not have default parameter on the far right.
I am not sure if I am right, though.

- All of those are incorrect.

- You cannot initialize any variables in a function prototype.

- Additionally, use code tags "< >" when posting code.
You are correct about why func1 is invalid. The rest is also invalid because temporary objects can't bind to non-const references. If you change all references to const, func2-func6 will be valid (but maybe not what you want).
1
2
3
4
5
void func2(int = 1, const int& = 2);
void func3(int*, const int& = 3);
void func4(const int& = 4, int* = 0);
void func5(const int& = 0, int = 1);
void func6(int = 5, const int& = 6, int* = 0);
Last edited on
thejman250 wrote:
You cannot initialize any variables in a function prototype.
That's incorrect. You can provide default values for variables in either the prototype or definition, but not both.
@Peter87

"The rest is also invalid because temporary objects can't bind to non-const references. If you change all references to const, func2-func6 will be valid."
I don't quite understand what you mean by temporary object, could you explain a bit or direct me to a reference or a page on some book?
@thejman250

I don't understand why we cannot initialize any variables in a function prototype, could you also explain a bit or show me a reference that explains it? I flip through C++ primer, but it does not say anything about such restriction...

I'll post code using <> in the future.
When you have an integer literal, like 2, that will create a temporary object. The temporary object will only exist on that line. After the line has ended the temporary object will no longer exist. Keeping a reference to the temporary object would allow you to access a non-existing object in the lines below. You don't want that. That is why this is not allowed:
1
2
int& r = 2;
cout << r; // Not good. The object that r is referring to no longer exists. 


const references are a bit different. You can use a const reference to extend the life time of the temporary object to have the same life time as the reference. Note that since it is a const reference you can't modify the object.
1
2
3
4
{
	const int& r = 2;
	cout << r; // 
} // r goes out of scope so the temporary object that r is referring to is destroyed here. 
thanks Peter.

So from what I understand so far, if I have void func4(int& = 4, int* = 0); the integral literal 4 is bound to some reference alias in the prototype, but it's not so in the function definition?

Thumper wrote:
That's incorrect. You can provide default values for variables in either the prototype or definition, but not both.


- Ok, go ahead and provide default values in the prototype without putting any variable names -_-.
You can also use names in a prototype if you prefer o_o
They're meaningless, but you can still use them.
Last edited on
@andrenvq57
If you have void func4(int& = 4, int* = 0); it should not even compile.
Thumper wrote:
You can also use names in a prototype if you prefer o_o
They're meaningless, but you can still use them.


- I highly doubt that what your saying is true, as i've never seen it done.

- However, i suppose that it's possible that you are actually correct.
@thejman250
What Thumper says is true.
Thumper wrote:
You can also use names in a prototype if you prefer o_o
They're meaningless, but you can still use them.


- I highly doubt that what your saying is true, as i've never seen it done.

- However, i suppose that it's possible that you are actually correct.


What? I do that all the time. How come you don't know that (I guess I didn't misunderstand anything)?
@Peter

I just tried, and it did not compiled at all. And all of them have such error: default argument for ‘int& <anonymous>’ has type ‘int’ . What does it have to do with inability to reference a temporary value?
Peter87 wrote:
@thejman250
What Thumper says is true.


- I see, well that's interesting.
@andrenvq57
As I said, you need const.
void func2(int = 1, const int& = 2);

There is probably not much point passing an int by const reference compared to passing a regular int.
void func2(int = 1, int = 2);

If you want the function to take a non-const reference, because you might want to modify the int variable that is passed to the function, you should probably not use default arguments for this function.
void func2(int, int&);
Last edited on
Topic archived. No new replies allowed.