0

Here is a silly little program (in my real application, 'iteration' was trying to scan a list doing something on each element).

#include <stdio.h>

void iteration(
        int& foo, int& bar,
        int (*callback)(int a,int b)) {
    while (foo<100) {
        int tmp = bar;
        bar = callback(foo, bar);
        foo = tmp;
        printf("%d\n", foo);
    }
}

void fibonacci(int foo, int bar) {
    int count = 0;
    iteration(
        foo, bar,
        [&](int a, int b) {
//          count += 1;
            return a+b;
        } );
    printf("count %d\n", count);
}

int main() {
fibonacci(1, 1);
}
// g++ -g -std=c++11 -o fib fib.cpp

When compiled (see comment on last line) and run, it prints a short Fibonnacci sequence followed, of course, by "count 0".

Now remove the "//" before the "count += 1;" and try again. It fails to compile with

$ g++ -g -std=c++11 -o fib fib.cpp
fib.cpp: In function ‘void fibonacci(int, int)’:
fib.cpp:21:5: error: cannot convert ‘fibonacci(int, int)::__lambda0’
to ‘int (*)(int, int)’ for argument ‘3’ to 
‘void iteration(int&, int&, int (*)(int, int))’
   } );
     ^

What am I doing wrong?

for my compiler version see

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.8/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v 
--with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' 
--with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs 
--enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.8 --enable-shared --enable-linker-build-id 
--libexecdir=/usr/lib --without-included-gettext 
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 
--libdir=/usr/lib --enable-nls --with-sysroot=/ 
--enable-clocale=gnu --enable-libstdcxx-debug 
--enable-libstdcxx-time=yes --enable-gnu-unique-object 
--disable-libmudflap --enable-plugin --with-system-zlib 
--disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo 
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-i386/jre 
--enable-java-home 
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-i386 
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-i386 
--with-arch-directory=i386 
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar 
--enable-objc-gc --enable-targets=all --enable-multiarch 
--disable-werror --with-arch-32=i686 
--with-multilib-list=m32,m64,mx32 --with-tune=generic 
--enable-checking=release 
--build=i686-linux-gnu --host=i686-linux-gnu 
--target=i686-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 

1 Answers1

2

Generally speaking, only capturless lambdas might be converted to function pointers.

Try using std::function instead:

void iteration(int& foo, int& bar, std::function<int(int, int)> callback) {
    while (foo<100) {
        int tmp = bar;
        bar = callback(foo, bar);
        foo = tmp;
        printf("%d\n", foo);
    }
}

P.S. I don't think your first program works well.

Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
  • OK, that worked just fine (though I haven't ported it to my real application yet). Thank you. Not sure why you thought my first program didn't work. It worked fine for me using C++11. I noted that your link was using c++17. – Charles Lindsey Feb 08 '18 at 22:57