function declaration with default parameter

Discussion in 'C++' started by Matthias Pfeifer, Jun 14, 2007.

  1. Hi there,

    I am trying to declare a function that takes a std::list<> parameter.
    I want this function to have an empty list as a default parameter.
    It's a template function also. Currently i am stuck because my
    compiler does not find a matching function when i do a call.

    here is my code:

    // ############## code begins
    #include <list>

    using namespace std;

    template<typename T>
    int* f(const int*,
    const int*,
    const int*,
    list<pair<int, T > > =list<pair<int, int > >() );

    int main (int argc, char* argv)
    {
    int* c, t, i;

    int* result = f(c, t, i);
    }

    template<typename T>
    int* f(const int* c, const int* t, const int* i,
    list<pair<int, T > > )
    {
    return (int *) 0;
    }
    // ############## code ends

    compiler says

    testcase.cpp: In function 'int main(int, char*)':
    testcase.cpp:16: error: no matching function for call to 'f(int*&, int&,
    int&)'

    compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
    here. Would be nice if someone knows how to do this right... thanks in
    advance.

    matthias
    Matthias Pfeifer, Jun 14, 2007
    #1
    1. Advertising

  2. Matthias Pfeifer

    Amal P Guest

    On Jun 14, 5:20 pm, Matthias Pfeifer <> wrote:
    > Hi there,
    >
    > I am trying to declare a function that takes a std::list<> parameter.
    > I want this function to have an empty list as a default parameter.
    > It's a template function also. Currently i am stuck because my
    > compiler does not find a matching function when i do a call.
    >
    > here is my code:
    >
    > // ############## code begins
    > #include <list>
    >
    > using namespace std;
    >
    > template<typename T>
    > int* f(const int*,
    > const int*,
    > const int*,
    > list<pair<int, T > > =list<pair<int, int > >() );
    >
    > int main (int argc, char* argv)
    > {
    > int* c, t, i;
    >
    > int* result = f(c, t, i);
    >
    > }
    >
    > template<typename T>
    > int* f(const int* c, const int* t, const int* i,
    > list<pair<int, T > > )
    > {
    > return (int *) 0;}
    >
    > // ############## code ends
    >
    > compiler says
    >
    > testcase.cpp: In function 'int main(int, char*)':
    > testcase.cpp:16: error: no matching function for call to 'f(int*&, int&,
    > int&)'
    >
    > compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
    > here. Would be nice if someone knows how to do this right... thanks in
    > advance.
    >
    > matthias


    Dear Matthias,

    The reason for the error is you didnt specify the type.

    A small modification can correct this problem.

    I just specified the type also before calling the function.

    #include <list>

    using namespace std;

    template<typename T>
    int* f(const int*,
    const int*,
    const int*,
    list<pair<int, T > > =list<pair<int, int > >() );

    int main (int argc, char* argv)
    {
    int* c, *t, *i;

    int* result = f<int>(c, t, i);

    }

    template<typename T>
    int* f(const int* c, const int* t, const int* i,
    list<pair<int, T > > )
    {
    return (int *) 0;
    }


    Thanks and regards,
    Amal P.
    Amal P, Jun 14, 2007
    #2
    1. Advertising

  3. Matthias Pfeifer

    Amal P Guest

    On Jun 14, 6:09 pm, Amal P <> wrote:
    > On Jun 14, 5:20 pm, Matthias Pfeifer <> wrote:
    >
    >
    >
    > > Hi there,

    >
    > > I am trying to declare a function that takes a std::list<> parameter.
    > > I want this function to have an empty list as a default parameter.
    > > It's a template function also. Currently i am stuck because my
    > > compiler does not find a matching function when i do a call.

    >
    > > here is my code:

    >
    > > // ############## code begins
    > > #include <list>

    >
    > > using namespace std;

    >
    > > template<typename T>
    > > int* f(const int*,
    > > const int*,
    > > const int*,
    > > list<pair<int, T > > =list<pair<int, int > >() );

    >
    > > int main (int argc, char* argv)
    > > {
    > > int* c, t, i;

    >
    > > int* result = f(c, t, i);

    >
    > > }

    >
    > > template<typename T>
    > > int* f(const int* c, const int* t, const int* i,
    > > list<pair<int, T > > )
    > > {
    > > return (int *) 0;}

    >
    > > // ############## code ends

    >
    > > compiler says

    >
    > > testcase.cpp: In function 'int main(int, char*)':
    > > testcase.cpp:16: error: no matching function for call to 'f(int*&, int&,
    > > int&)'

    >
    > > compiler version is g++ 4.0.2. I guess i am doing wrong with the syntax
    > > here. Would be nice if someone knows how to do this right... thanks in
    > > advance.

    >
    > > matthias

    >
    > Dear Matthias,
    >
    > The reason for the error is you didnt specify the type.
    >
    > A small modification can correct this problem.
    >
    > I just specified the type also before calling the function.
    >
    > #include <list>
    >
    > using namespace std;
    >
    > template<typename T>
    > int* f(const int*,
    > const int*,
    > const int*,
    > list<pair<int, T > > =list<pair<int, int > >() );
    >
    > int main (int argc, char* argv)
    > {
    > int* c, *t, *i;
    >
    > int* result = f<int>(c, t, i);
    >
    > }
    >
    > template<typename T>
    > int* f(const int* c, const int* t, const int* i,
    > list<pair<int, T > > )
    > {
    > return (int *) 0;
    >
    > }
    >
    > Thanks and regards,
    > Amal P.


    Dear Matthias,

    I hope you write this code just to test the default parameter.
    It wont be possible to run this code now also because none of the
    pointers point to a valid memory.

    Thanks and regards,
    Amal P.
    Amal P, Jun 14, 2007
    #3
  4. > Dear Matthias,
    >
    > I hope you write this code just to test the default parameter.
    > It wont be possible to run this code now also because none of the
    > pointers point to a valid memory.
    >
    > Thanks and regards,
    > Amal P.


    Hi Amal,

    you are right. The code was just for demonstration. Thank you for
    your accurate answer. My program works now. Btw: Do you know why
    it is not possible to give a default parameter to the template parameter?

    template<typename T=int> ...

    would look nice for me.

    Matthias
    Matthias Pfeifer, Jun 14, 2007
    #4
  5. Matthias Pfeifer

    Amal P Guest

    On Jun 14, 7:45 pm, Matthias Pfeifer <> wrote:
    > > Dear Matthias,

    >
    > > I hope you write this code just to test the default parameter.
    > > It wont be possible to run this code now also because none of the
    > > pointers point to a valid memory.

    >
    > > Thanks and regards,
    > > Amal P.

    >
    > Hi Amal,
    >
    > you are right. The code was just for demonstration. Thank you for
    > your accurate answer. My program works now. Btw: Do you know why
    > it is not possible to give a default parameter to the template parameter?
    >
    > template<typename T=int> ...
    >
    > would look nice for me.
    >
    > Matthias


    Dear Matthias,

    As you know template is a mechanism which we can use for easiness
    to support different datatype, if you always want it as an int, why
    you still use a template?
    You should not give like that because it does not worth. If always
    you are giving an int there is there any use for using a template
    there?

    I am sorry if i have mistaken your question.

    Thanks and regards,
    Amal P.
    Amal P, Jun 14, 2007
    #5
  6. Matthias Pfeifer

    dasjotre Guest

    On 14 Jun, 09:20, Matthias Pfeifer <> wrote:
    > #include <list>
    >
    > using namespace std;
    >
    > template<typename T>
    > int* f(const int*,
    > const int*,
    > const int*,
    > list<pair<int, T > > =list<pair<int, int > >() );
    >
    > int main (int argc, char* argv)


    either
    int main(int argc, char* argv[])
    or
    int main()

    > {
    > int* c, t, i;
    >
    > int* result = f(c, t, i);


    c is a pointer to int but both t and i are just ints.

    also

    int* c, *t, *i;
    int* result = f<int>(c, t, i);

    will work but but only for int

    int* result = f<char>(c, t, i);

    will not, since you can not convert
    pair<int,char> to pair<int, int>

    one way would be to provide template
    version

    template<typename T>
    int* f(const int*,
    const int*,
    const int*,
    list<pair<int, T > > = list<pair<int, T > >() );

    and a non template version for int

    int* f(const int*,
    const int*,
    const int*,
    list<pair<int, int > > = list<pair<int, int > >() );

    and then call them as
    int* result = f(c, t, i); // calls int version
    int* result1 = f<char>(c, t, i); // calls template version

    your compiler might allow it, but main should
    return a value

    regards

    DS
    dasjotre, Jun 14, 2007
    #6
  7. Matthias Pfeifer

    dasjotre Guest

    On 14 Jun, 12:49, dasjotre <> wrote:
    > your compiler might allow it, but main should
    > return a value


    I meant, your compiler might allow you to
    omit return statement, but all functions
    declared as returning anything but void
    must have a return statement on all
    possible execution paths.

    DS
    dasjotre, Jun 14, 2007
    #7
  8. Matthias Pfeifer

    Gavin Deane Guest

    On 14 Jun, 12:53, dasjotre <> wrote:
    > On 14 Jun, 12:49, dasjotre <> wrote:
    >
    > > your compiler might allow it, but main should
    > > return a value


    Your compiler must allow the omission of the return statement it in
    the specific case of main.

    > I meant, your compiler might allow you to
    > omit return statement, but all functions
    > declared as returning anything but void
    > must have a return statement on all
    > possible execution paths.


    There is an explicit exclusion for main(). If you reach the end of the
    function and there is no return statement, the compiler assumes return
    0;

    Gavin Deane
    Gavin Deane, Jun 14, 2007
    #8
  9. Matthias Pfeifer

    Marcus Kwok Guest

    dasjotre <> wrote:
    > On 14 Jun, 12:49, dasjotre <> wrote:
    >> your compiler might allow it, but main should
    >> return a value

    >
    > I meant, your compiler might allow you to
    > omit return statement, but all functions
    > declared as returning anything but void
    > must have a return statement on all
    > possible execution paths.


    main() is special, in that the Standard explicitly allows the return
    statement to be omitted, with the result that it will implicitly return
    0 to the environment.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jun 14, 2007
    #9
  10. Matthias Pfeifer wrote:
    >> Dear Matthias,
    >>
    >> I hope you write this code just to test the default parameter.
    >> It wont be possible to run this code now also because none of the
    >> pointers point to a valid memory.
    >>
    >> Thanks and regards,
    >> Amal P.

    >
    > Hi Amal,
    >
    > you are right. The code was just for demonstration. Thank you for
    > your accurate answer. My program works now. Btw: Do you know why
    > it is not possible to give a default parameter to the template parameter?
    >
    > template<typename T=int> ...
    >
    > would look nice for me.
    >
    > Matthias


    The committee agrees with you and the next C++ revision will allow that
    ;). For now, you can use an overload to surcome the need to specify the
    template argument:

    int *f(const int* a, const int* b, const int* c)
    {
    return f(a,b,c,list<pair<int,int> >());
    }

    - Sylvester
    Sylvester Hesp, Jun 14, 2007
    #10
  11. Matthias Pfeifer

    dasjotre Guest

    On 14 Jun, 14:58, (Marcus Kwok) wrote:
    > dasjotre <> wrote:
    > > On 14 Jun, 12:49, dasjotre <> wrote:
    > >> your compiler might allow it, but main should
    > >> return a value

    >
    > > I meant, your compiler might allow you to
    > > omit return statement, but all functions
    > > declared as returning anything but void
    > > must have a return statement on all
    > > possible execution paths.

    >
    > main() is special, in that the Standard explicitly allows the return
    > statement to be omitted, with the result that it will implicitly return
    > 0 to the environment.
    >


    you're absolutely right, I can't think of reason why it should
    be so thou. can you?

    regards

    DS
    dasjotre, Jun 15, 2007
    #11
  12. Matthias Pfeifer

    Marcus Kwok Guest

    dasjotre <> wrote:
    > On 14 Jun, 14:58, (Marcus Kwok) wrote:
    >> main() is special, in that the Standard explicitly allows the return
    >> statement to be omitted, with the result that it will implicitly return
    >> 0 to the environment.

    >
    > you're absolutely right, I can't think of reason why it should
    > be so thou. can you?


    I can't say for certain (maybe you should ask on comp.std.c++), but my
    intuition would be to help make non-conformant code using

    void main() { /* ... */ }

    legal just by changing it to

    int main() { /* ... */ }

    But then, if you're changing the return type from "void" to "int", it's
    not that much extra work just to add a "return 0;" to the bottom either.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Jun 15, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ovidesvideo
    Replies:
    4
    Views:
    481
    Andrey Tarasevich
    Dec 10, 2004
  2. Replies:
    4
    Views:
    1,051
    Richard Tobin
    Dec 12, 2006
  3. Stuart Redmann
    Replies:
    5
    Views:
    468
    Stuart Redmann
    Dec 14, 2007
  4. Luca Forlizzi
    Replies:
    4
    Views:
    446
    Luca Forlizzi
    Nov 14, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    162
Loading...

Share This Page