CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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

  2. #2
    Join Date
    May 2009
    Posts
    4

    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

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    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&);
    };

  4. #4
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Do default arguments have to be constant expressions?

    Quote Originally Posted by HighCommander4 View Post
    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.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Do default arguments have to be constant expressions?

    Quote Originally Posted by JohnW@Wessex View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured