http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59274

            Bug ID: 59274
           Summary: Problem with passing std::function object as value
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ks at edaboard dot com

During learning of new features of c++11 I tested this code to print random
numbers, with the function object passed as reference this code works as
expected:

#include <iostream>
#include <functional>
#include <random>
#include <chrono>

void print_random(std::function<double()>& dice) {
    std::cout << dice() << ", ";
} 

int main(int argc, char **argv) {
    std::default_random_engine
generator(std::chrono::system_clock::now().time_since_epoch().count());
    std::normal_distribution<double> distribution(0,6);
    std::function<double()> dice = std::bind (distribution, generator);
    std::cout << "Random numbers: ";
    for(int i=0; i<30; ++i) print_random(dice);
    std::cout << std::endl;
    return 0;
}

The output:
Random numbers: -3.02879, 10.0929, -0.0317818, 4.81011, 1.97929, -0.392321,
-7.58843, -1.29167, -5.95159, -5.817, 6.94564, -12.699, -0.360104, -3.74067,
-3.17531, -2.09552, 1.23506, 4.49736, -0.656716, 16.852, 1.94649, 8.01674,
-4.61985, -16.5746, 3.07316, 0.123368, 3.8882, -3.00898, -8.77073, 1.76514,

but after I change the print_random function in order to pass the dice
parameter as value:
void print_random(std::function<double()> dice) {

The result is the same random value repeated 30 times like this:
Random numbers: 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122,
2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122,
2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122,
2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122, 2.07122,


Compile command: "g++ -O3 -std=c++11 a.cpp -o a"

g++ -v:
"Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.8.1-10ubuntu8' --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 --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-amd64/jre
--enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64
--with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar
--enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8)"

Reply via email to