Boost logo

Boost Users :

Subject: [Boost-users] [Function] default arguments, member func vs. free func
From: Mike Lundy (mike_at_[hidden])
Date: 2008-11-04 02:35:29


I came across a difference in behavior for Boost.Function between g++
4.2 and 4.3, and I was wondering whether my usage was incorrect. I'm using
default parameters in much the same way as the sample test.cc below. It
compiles on 4.2, and does not on 4.3. I think I can see why perhaps it
shouldn't have worked, but I'm just looking for a security blanket. If my
usage is wrong, it might be worth a mention in the docs, since (to me) either
answer is non-intuitive.

g++-4.2 4.2.4 (4.2.4-3ubuntu4)
g++-4.3 4.3.2 (4.3.2-1ubuntu11)
boost 1.35 (1.35.0-8ubuntu1)

cat << EOF > test.cc
#include <iostream>
#include <boost/function.hpp>

struct add1 {
    int operator()(int x, int y = 1) const { return x + y; }
};

int add2(int x, int y = 1) { return x + y; }

int main()
{
    boost::function <int (int x)> adder1 = add1();
    boost::function <int (int x)> adder2 = add2; // compile error here

    std::cout << adder1(1) << adder2(1) << std::endl;
    return 0;
}
EOF

mike_at_hyperion:~> g++-4.2 -Wall -Wextra test.cc -o test && ./test
22
mike_at_hyperion:~> g++-4.3 -Wall -Wextra test.cc -o test && ./test

/usr/include/boost/function/function_template.hpp: In static member
function 'static R
boost::detail::function::function_invoker1<FunctionPtr, R,
T0>::invoke(boost::detail::function::function_buffer&, T0) [with
FunctionPtr = int (*)(int, int), R = int, T0 = int]':

/usr/include/boost/function/function_template.hpp:787: instantiated
from 'void boost::function1<R, T0, Allocator>::assign_to(const
Functor&) [with Functor = int (*)(int, int), R = int, T0 = int,
Allocator = std::allocator<void>]'

/usr/include/boost/function/function_template.hpp:624: instantiated
from 'boost::function1<R, T0, Allocator>::function1(Functor, typename
boost::enable_if_c<boost::type_traits::ice_not::value, int>::type)
[with Functor = int (*)(int, int), R = int, T0 = int, Allocator =
std::allocator<void>]'

/usr/include/boost/function/function_template.hpp:886: instantiated
from 'boost::function<R ()(T0), Allocator>::function(Functor, typename
boost::enable_if_c<boost::type_traits::ice_not::value, int>::type)
[with Functor = int (*)(int, int), R = int, T0 = int, Allocator =
std::allocator<void>]'

test.cc:13: instantiated from here

/usr/include/boost/function/function_template.hpp:100: error: too few
arguments to function


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net