-
May 27th, 2009, 12:24 AM
#1
Do default arguments have to be constant expressions?
Hello everyone, it's been a while
Came across a compiler error today that I found strange:
Code:
class foo
{
public:
void method(int x = value) {}
private:
int value;
};
MinGW says: invalid use of non-static data member `foo::value'
Why would the data member being non-static be a problem? After all, the method is also non-static...
Could it be that default arguments to functions have to be constant expressions? (Come to think of it, I don't recall having to use something other than a constant expression in a default argument before).
Old Unix programmers never die, they just mv to /dev/null
-
May 27th, 2009, 02:28 AM
#2
Re: Do default arguments have to be constant expressions?
foo just is a class,if you want to use value ,you need an object
for example
foo fooobject;
fooobject.value
-
May 27th, 2009, 07:36 AM
#3
Re: Do default arguments have to be constant expressions?
Why would the data member being non-static be a problem? After all, the method is also non-static...
A member function doesn't have a access to the this pointer until you actually enter the body of the function, but the function's arguments must be pushed onto the stack [i]before[i] that function is called. About the only way you're going to be able to do something like this is if the class in question is a singleton:
Code:
class Foo
{
public:
static Foo& GetSingleton( )
{
static Foo instance;
return instance;
}
void Frobnicate(int i = GetSingleton( ).var) { }
private:
int var;
Foo( ) { }
Foo(Foo&);
Foo& operator =(Foo&);
};
-
May 27th, 2009, 07:58 AM
#4
Re: Do default arguments have to be constant expressions?
Originally Posted by HighCommander4
Could it be that default arguments to functions have to be constant expressions? (Come to think of it, I don't recall having to use something other than a constant expression in a default argument before).
No, they don't. The code below is valid, for example:
Code:
struct A {};
A F1() { return A(); }
void F2(A const& a = F1()) {}
int main()
{
F2();
}
The point is that you cannot use a nonstatic member as a default argument directly. Think of it for a while. There's no way a compiler can decide from which foo object your value member refers too. Remember that even member functions are somehow implemented as free functions but with an addition implicit parameter that corresponds to the this object.
-
May 27th, 2009, 08:00 AM
#5
Re: Do default arguments have to be constant expressions?
How about just overloading the function.
Code:
class foo
{
public:
void method(int x)
{
}
void method()
{
method(value);
}
private:
int value;
};
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
May 27th, 2009, 08:47 AM
#6
Re: Do default arguments have to be constant expressions?
Originally Posted by JohnW@Wessex
How about just overloading the function.
Code:
class foo
{
public:
void method(int x)
{
}
void method()
{
method(value);
}
private:
int value;
};
Yup, that would be the natural alternative.
You'd think compiler writers might have implemented using the nonstatic member as a default value, as a shorthand for the above code, though.
Old Unix programmers never die, they just mv to /dev/null
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
|
Click Here to Expand Forum to Full Width
|