defaults for template'd functions

Discussion in 'C++' started by krishnaroskin@gmail.com, Jan 13, 2007.

  1. Guest

    Hey all,

    I've been running into a problem with default values to template'd
    functions. I've boiled down my problem to this simple example code:

    #include<iostream>

    using namespace std;

    // function object
    template<typename Value> struct function {
    void operator()(Value v) {
    cout << '*' << v << '*' << endl;
    }
    };

    template<typename Value, typename Function>
    void do_it(Value v, Function f = function<Value>()) {
    cout << v << '\t';
    f(v);
    }

    int main(int argc, char* argv[]) {
    do_it(3);
    return 0;
    }

    Basically, I want the second parameter of do_it to be anything that can
    be called like a function with a single argument of type Value but I
    want the default to be the default constructed object of type
    function<Value>(). That's what I want, but what I get is this error:

    test.cpp:19: error: no matching function for call to 'do_it(int)'

    It doesn't seem to be recognizing my default. If I change the call to
    do_it to:

    do_it<int, function<int> >(3);

    i.e explicitly give the type of the default in int case, it works fine.
    But I don't want to have to give it the default type every time I call
    it, then I wouldn't even bother with making it a default.

    So, I figured if I give a default for the typename and well as the
    value it might work:

    template<typename Value, typename Function = function<Value> >
    void do_it(Value v, Function f = function<Value>()) {
    cout << v << '\t';
    f(v);
    }

    but then I get this error:

    test.cpp:13: error: default template arguments may not be used in
    function templates

    Any idea why none of this isn't working? Am I asking C++ to do to much
    type inference? I also don't understand why I'm getting the error at
    the function call and not the function declaration.

    P.S. I'm compiling this with gcc version 4.0.1 (Apple Computer, Inc.
    build 5250). Here's the full g++ -v:

    Using built-in specs.
    Target: i686-apple-darwin8
    Configured with: /private/var/tmp/gcc/gcc-5250.obj~12/src/configure
    --disable-checking -enable-werror --prefix=/usr --mandir=/share/man
    --enable-languages=c,objc,c++,obj-c++
    --program-transform-name=/^[cg][^.-]*$/s/$/-4.0/
    --with-gxx-include-dir=/include/c++/4.0.0 --build=powerpc-apple-darwin8
    --with-arch=pentium-m --with-tune=prescott --program-prefix=
    --host=i686-apple-darwin8 --target=i686-apple-darwin8
    Thread model: posix
    gcc version 4.0.1 (Apple Computer, Inc. build 5250)

    -krish
    , Jan 13, 2007
    #1
    1. Advertising

  2. Greg Guest

    wrote:
    > Hey all,
    >
    > I've been running into a problem with default values to template'd
    > functions. I've boiled down my problem to this simple example code:
    >
    > #include<iostream>
    >
    > using namespace std;
    >
    > // function object
    > template<typename Value> struct function {
    > void operator()(Value v) {
    > cout << '*' << v << '*' << endl;
    > }
    > };
    >
    > template<typename Value, typename Function>
    > void do_it(Value v, Function f = function<Value>()) {
    > cout << v << '\t';
    > f(v);
    > }
    >
    > int main(int argc, char* argv[]) {
    > do_it(3);
    > return 0;
    > }
    >
    > Basically, I want the second parameter of do_it to be anything that can
    > be called like a function with a single argument of type Value but I
    > want the default to be the default constructed object of type
    > function<Value>(). That's what I want, but what I get is this error:
    >
    > test.cpp:19: error: no matching function for call to 'do_it(int)'


    The problem is that there is now way to figure out the type of the
    second type parameter in the "do_it(3)" function call. After all,
    main() calls do_it() with only one argument, 3. So the compiler has to
    figure out what type a second argument would have been - if main() had
    called do_it() with two arguments (instead the one argument) that
    main() passed in the actual call.

    Of course, there is no way to ascertain the type of an absent argument
    - nor can the program ever be able to answer that question. So another
    approach is needed to solve this problem. And that solution would be to
    have the program provide a second parameter whenever one is not present
    in the call to do_it(). In this way the program can pick whatever type
    it likes that argument to be (in the case, the choice will be
    function<Value>):

    #include <iostream>

    using std::cout;
    using std::endl;

    template< class Value>
    struct function
    {
    void operator()(Value v)
    {
    cout << '*' << v << '*' << endl;
    }
    };

    template< class Value, class Function>
    void do_it( Value v, Function f )
    {
    cout << v << '\t';
    f(v);
    }

    template< class Value>
    void do_it( Value v)
    {
    do_it( v, function<Value>());
    }

    int main()
    {
    do_it(3);
    }

    With two do_it() overloads, calling do_it() with one argument forwards
    the call to the other do_it(), providing its own the Function<Value>
    object as the "missing", second parameter. Essentially, the program has
    implemented C++'s default argument behavior for the do_it() function
    all on its own.

    Greg
    Greg, Jan 13, 2007
    #2
    1. Advertising

  3. Greg Guest

    wrote:
    > Hey all,
    >
    > I've been running into a problem with default values to template'd
    > functions. I've boiled down my problem to this simple example code:
    >
    > #include<iostream>
    >
    > using namespace std;
    >
    > // function object
    > template<typename Value> struct function {
    > void operator()(Value v) {
    > cout << '*' << v << '*' << endl;
    > }
    > };
    >
    > template<typename Value, typename Function>
    > void do_it(Value v, Function f = function<Value>()) {
    > cout << v << '\t';
    > f(v);
    > }
    >
    > int main(int argc, char* argv[]) {
    > do_it(3);
    > return 0;
    > }
    >
    > Basically, I want the second parameter of do_it to be anything that can
    > be called like a function with a single argument of type Value but I
    > want the default to be the default constructed object of type
    > function<Value>(). That's what I want, but what I get is this error:
    >
    > test.cpp:19: error: no matching function for call to 'do_it(int)'


    The problem is that there is no way to figure out the type of the
    second type parameter in the "do_it(3)" function call. After all,
    main() calls do_it() with only one argument, 3. So the compiler has to
    figure out what type a second argument would have been - if main() had
    called do_it() with two arguments (instead the one argument) that
    main() passed in the actual call.

    Of course, there is no way to ascertain the type of an absent argument
    - nor can the program ever be able to answer that question. So another
    approach is needed to solve this problem. And that solution would be to
    have the program provide a second parameter whenever one is not present
    in the call to do_it(). In this way the program can pick whatever type
    it likes that argument to be (in the case, the choice will be
    function<Value>):

    #include <iostream>

    using std::cout;
    using std::endl;

    template< class Value>
    struct function
    {
    void operator()(Value v)
    {
    cout << '*' << v << '*' << endl;
    }
    };

    template< class Value, class Function>
    void do_it( Value v, Function f )
    {
    cout << v << '\t';
    f(v);
    }

    template< class Value>
    void do_it( Value v)
    {
    do_it( v, function<Value>());
    }

    int main()
    {
    do_it(3);
    }

    With two do_it() overloads, calling do_it() with one argument forwards
    the call to the other do_it(), providing its own the Function<Value>
    object as the "missing", second parameter. Essentially, the program has
    implemented C++'s default argument behavior for the do_it() function
    all on its own.

    Greg
    Greg, Jan 13, 2007
    #3
  4. Guest

    On Jan 12, 10:01 pm, "Greg" <> wrote:
    > wrote:
    > > Hey all,

    >
    > > I've been running into a problem with default values to template'd
    > > functions. I've boiled down my problem to this simple example code:

    >
    > > #include<iostream>

    >
    > > using namespace std;

    >
    > > // function object
    > > template<typename Value> struct function {
    > > void operator()(Value v) {
    > > cout << '*' << v << '*' << endl;
    > > }
    > > };

    >
    > > template<typename Value, typename Function>
    > > void do_it(Value v, Function f = function<Value>()) {
    > > cout << v << '\t';
    > > f(v);
    > > }

    >
    > > int main(int argc, char* argv[]) {
    > > do_it(3);
    > > return 0;
    > > }

    >
    > > Basically, I want the second parameter of do_it to be anything that can
    > > be called like a function with a single argument of type Value but I
    > > want the default to be the default constructed object of type
    > > function<Value>(). That's what I want, but what I get is this error:

    >
    > > test.cpp:19: error: no matching function for call to 'do_it(int)'

    >
    > The problem is that there is no way to figure out the type of the
    > second type parameter in the "do_it(3)" function call. After all,
    > main() calls do_it() with only one argument, 3. So the compiler has to
    > figure out what type a second argument would have been - if main() had
    > called do_it() with two arguments (instead the one argument) that
    > main() passed in the actual call.
    >
    > Of course, there is no way to ascertain the type of an absent argument
    > - nor can the program ever be able to answer that question.


    That makes sense. And the compiler doesn't want to infer the 'default'
    type from the default value of the second parameter?

    Then in theory, I should be able to give a default type for the
    Function template parameter and it should work? Or is there some reason
    that wouldn't work as well? And why won't gcc let you give defaults to
    template parameters for functions? Is there a fundamental reason it
    doesn't allow this?

    > So another approach is needed to solve this problem. And that solution
    > would be to have the program provide a second parameter whenever one
    > is not present in the call to do_it(). In this way the program can pick whatever
    > type it likes that argument to be (in the case, the choice will be
    > function<Value>):
    >
    > #include <iostream>
    >
    > using std::cout;
    > using std::endl;
    >
    > template< class Value>
    > struct function
    > {
    > void operator()(Value v)
    > {
    > cout << '*' << v << '*' << endl;
    > }
    > };
    >
    > template< class Value, class Function>
    > void do_it( Value v, Function f )
    > {
    > cout << v << '\t';
    > f(v);
    > }
    >
    > template< class Value>
    > void do_it( Value v)
    > {
    > do_it( v, function<Value>());
    > }
    >
    > int main()
    > {
    > do_it(3);
    > }
    >
    > With two do_it() overloads, calling do_it() with one argument forwards
    > the call to the other do_it(), providing its own the Function<Value>
    > object as the "missing", second parameter. Essentially, the program has
    > implemented C++'s default argument behavior for the do_it() function
    > all on its own.


    Thanks a lot for your help and solution!

    -krish
    , Jan 13, 2007
    #4
  5. Greg Guest

    wrote:
    > That makes sense. And the compiler doesn't want to infer the 'default'
    > type from the default value of the second parameter?
    >
    > Then in theory, I should be able to give a default type for the
    > Function template parameter and it should work? Or is there some reason
    > that wouldn't work as well? And why won't gcc let you give defaults to
    > template parameters for functions? Is there a fundamental reason it
    > doesn't allow this?


    Default type parameters for function templates have been added to the
    C++ working draft. So I would expect them to be part of the next C++
    language Standard. So your solution should work in the future - just
    probably not today - with your current C++ compiler.

    Greg
    Greg, Jan 13, 2007
    #5
  6. Greg Guest

    wrote:
    > That makes sense. And the compiler doesn't want to infer the 'default'
    > type from the default value of the second parameter?
    >
    > Then in theory, I should be able to give a default type for the
    > Function template parameter and it should work? Or is there some reason
    > that wouldn't work as well? And why won't gcc let you give defaults to
    > template parameters for functions? Is there a fundamental reason it
    > doesn't allow this?


    Default type parameters for function templates have been added to the
    C++ working draft. So I would expect them to be part of the next C++
    language Standard. So your suggested solution (not the original
    program) should be possible in the future - just probably not today -
    with your current C++ compiler.

    Greg
    Greg, Jan 13, 2007
    #6
    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. Mark Fox

    Changing VS.NET defaults

    Mark Fox, Jul 31, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    365
    Yan-Hong Huang[MSFT]
    Aug 4, 2003
  2. Lauchlan M
    Replies:
    2
    Views:
    494
    John Saunders
    Aug 17, 2003
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,595
    Victor Bazarov
    Jun 21, 2004
  4. Hicham Mouline
    Replies:
    0
    Views:
    432
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    415
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page