somefunc(int& a = 4) -> default argument for ‘int& a’ has type ‘int’

This is a discussion on somefunc(int& a = 4) -> default argument for ‘int& a’ has type ‘int’ within the C++ Programming forums, part of the General Programming Boards category; Is there a solution for it that isn't playing with pointers? My aim is to have a function that would ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    somefunc(int& a = 4) -> default argument for ‘int& a’ has type ‘int’

    Is there a solution for it that isn't playing with pointers?

    My aim is to have a function that would take an int as a parameter by reference, but when no int is passed, a default int is given, but that default has to be by reference too - i don't know how to achieve it though.
    Programming is a form of art.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    My aim is to have a function that would take an int as a parameter by reference, but when no int is passed, a default int is given, but that default has to be by reference too - i don't know how to achieve it though.
    You are asking for the impossible. Consider: no int is passed, so the default int value is used. The function modifies the value of the argument. Effectively, the default int value has changed! This is powerful, as it allows you to make the value of say, 0 become 1 (i.e., the int literal 0 becomes the int literal 1, meaning that 0 == 1 is true).

    Perhaps you actually do not want to modify the argument, thus passing by value or const reference will do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't take a reference of a constant, so if you did "somefunc(4)" with the above declaration (without the default) it would also fail.

    The reason to have a reference is that you will be able to modify the passed in variable. Since a constant can't be changed, it's kind of meaningless to pass in a constant as a reference, even if the compiler would allow it.

    We may be able to give you an option if you explain further what you actually are trying to achieve with this.

    One alternative is of course to declare two functions:
    Code:
    void somefunc(int &a) 
    {
        // this is called with one integer parameter. 
       ... 
    }
    
    void somefunc()
    {
       // This is called when no parameter is given.
       ... 
    }
    --
    Mats

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You can't have a reference to a literal value, therefore having a default argument for a reference doesn't make much sense.

    One thing you could do (I don't reccomend doing this because it involves global variables) is make your reference have a default variable to refer to
    Code:
    #include <iostream>
    
    int default_arg = 10;
    void foo(int& bar = default_arg)
    {
        std::cout << bar << std::endl;
    }
    
    int main()
    {
        int i = 5;
        foo(i);
        foo();
    }
    Or, you could just overload your functions (This is a much better solution)
    Code:
    #include <iostream>
    
    void foo(int& bar)
    {
        std::cout << bar << std::endl;
    }
    
    void foo()
    {
        std::cout << 10 << std::endl;
    }
    
    int main()
    {
        int i = 5;
        foo(i);
        foo();
    }
    Last edited by Bench82; 08-28-2007 at 01:57 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    actually, it's a bit more complicated, a vector gets passed, but i brought a simple example with int.

    when no vector is passed, an empty, default, vector is given and the for cycle(that handles the vector) will not modify anything - since it can be a constant - the for cycle wouldn't mind.

    And yes, maybe I am asking the impossible, cause I don't even know what a "literal" means.
    Programming is a form of art.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A "literal" is a constant value, such as 4, 36, "A string", 'a' or anything else that is always going to result in the same value when compiled.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    If you don't want to modify the vector/int, then make it a reference to const.

    If you want to modify the vector (or int) then you can't do it (although I think maybe VC++ might allow it as an extension). You'll have to use pointers. But this is an example of where a pointer makes sense. You have an optional argument. So use a pointer defaulted to null, and that will indicate whether the user intended to pass an object for modification.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    oh, ok. thanks for explaining.

    would this work:

    somefunc(int& a = 4 [a not to compiler: I will not change the literal, honestly. Please, don't whine ])
    Programming is a form of art.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It might. It depends on your compiler settings. Some modern compilers have a LISTEN_TO_BEGGING flag that could allow that to work.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    oh, ok. thanks for explaining.

    would this work:

    somefunc(int& a = 4 [a not to compiler: I will not change the literal, honestly. Please, don't whine ])
    Yes, and this is how you do it:
    Code:
    void sumfunc(const int& a = 4);
    If you are planning to modify the argument, then there must be something wrong with your logic.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by matsp View Post
    You can't take a reference of a constant, so if you did "somefunc(4)" with the above declaration (without the default) it would also fail.
    You can definitely take a reference of a constant. The reference must be "const" in order to do it.

    Code:
    const int &a = 5;

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> If you are planning to modify the argument, then there must be something wrong with your logic.

    It is an optional argument that needs to be modified, but the OP just wants to know if that can be implemented with references. The logic is fine but it just won't work with references.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Write a 2nd function:
    Code:
    void SomeFunc( const vector<blah>&  vec )
    {
       ...
    }
    
    void SomeFunc()
    {
       vector<blah> vec;
       SomeFunc( vec );
    }

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    Quote Originally Posted by Daved View Post
    >> If you are planning to modify the argument, then there must be something wrong with your logic.

    It is an optional argument that needs to be modified, but the OP just wants to know if that can be implemented with references. The logic is fine but it just won't work with references.
    I'm not convinced of that; a function that accepts an argument by non-const reference, and modifies an argument that is not passed (should that be allowed) would reasonably be expected to exhibit spurious behaviour.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by Bench82 View Post
    You can't have a reference to a literal value, therefore having a default argument for a reference doesn't make much sense.

    One thing you could do (I don't reccomend doing this because it involves global variables) is make your reference have a default variable to refer to
    Code:
    #include <iostream>
    
    int default_arg = 10;
    void foo(int& bar = default_arg)
    {
        std::cout << bar << std::endl;
    }
    
    int main()
    {
        int i = 5;
        foo(i);
        foo();
    }
    Or, you could just overload your functions (This is a much better solution)
    Code:
    #include <iostream>
    Well, the global variable isn't a problem. The whole thingy lies in a class and I could make a member which is actually an empty vector(I am actually dealing with vectors instead of ints) and make it a default argument.
    an ingenious solution imho. thanks.

    btw, I reverted back to using pointers - it's more straightforward and can support my aims - everything works fine.

    thanks to everyone.
    Programming is a form of art.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  2. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 05:17 AM
  3. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21