Problem converting template function argument to boost::function

Discussion in 'C++' started by eefacm@gmail.com, Jan 24, 2008.

  1. Guest

    I've been experimenting with Boost.Function. I wrote a little test
    program, of which this is the guts:

    template <typename T>
    void call_with_arg(function<void (T)> func, T arg) {
    func(arg);
    }

    void print_it(int x) {
    cout << "printit: " << x << endl;
    }

    int main() {
    call_with_arg(print_it, 1);
    }

    When I compile this with g++ 4.0.1, I get this error:

    a.cc: In function 'int main()':
    a.cc:24: error: no matching function for call to 'call_with_arg(void
    (&)(int), int)'

    I can't figure out why. It works if I say:

    call_with_arg<int>(print_it, 1);

    Or:

    function<void (int)> func = print_it;
    call_with_arg(func, 1);

    So why doesn't the first way work?
    , Jan 24, 2008
    #1
    1. Advertising

  2. Barry Guest

    wrote:
    > I've been experimenting with Boost.Function. I wrote a little test
    > program, of which this is the guts:
    >
    > template <typename T>
    > void call_with_arg(function<void (T)> func, T arg) {
    > func(arg);
    > }
    >
    > void print_it(int x) {
    > cout << "printit: " << x << endl;
    > }
    >
    > int main() {
    > call_with_arg(print_it, 1);
    > }
    >
    > When I compile this with g++ 4.0.1, I get this error:
    >
    > a.cc: In function 'int main()':
    > a.cc:24: error: no matching function for call to 'call_with_arg(void
    > (&)(int), int)'
    >
    > I can't figure out why. It works if I say:
    >
    > call_with_arg<int>(print_it, 1);
    >
    > Or:
    >
    > function<void (int)> func = print_it;
    > call_with_arg(func, 1);
    >
    > So why doesn't the first way work?


    Because conversion isn't involved for function template argument deduction.

    call_with_arg(print_it, 1); needs to convert print_it into
    function<void(int)>.
    Barry, Jan 25, 2008
    #2
    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. Jim West
    Replies:
    3
    Views:
    758
    Jim West
    Oct 7, 2004
  2. Vijai Kalyan
    Replies:
    4
    Views:
    705
    Vijai Kalyan
    Nov 8, 2005
  3. nw
    Replies:
    0
    Views:
    314
  4. nguillot
    Replies:
    5
    Views:
    530
  5. Christopher
    Replies:
    1
    Views:
    810
    Yakov Gerlovin
    Oct 5, 2011
Loading...

Share This Page