Boost logo

Boost :

Subject: Re: [boost] [local] Help for the Alternatives section
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-05-08 17:00:24


On Sun, May 8, 2011 at 1:06 PM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
> On Sun, May 8, 2011 at 11:22 AM, Mathias Gaunard
>>
>> The type erasure appear unnecessary, and this implementation has big
>> forwarding problems.
>>
>> What about
>
> Yes, makes sense. Using your suggestion:

Hi all, the following code compiles and runs just fine on GCC but it
given the error below on MSVC. The error seems to be about a
conversion to boost::function_base inside Boost.Function...

#include <boost/local/function.hpp>
#include <boost/function.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

namespace boost { namespace local { namespace function {

template<typename F0, typename F1, typename F2 = void, typename F3 = void>
struct overload {};

// Specialization for 2 functions.

template<typename F0, typename F1>
struct overload<F0, F1>: F0, F1 {
    overload(F0 const& f0, F1 const& f1):
            F0(f0), F1(f1) {}
    using F0::operator(); using F1::operator();
};

}}} // namespace boost::local::function

// More specializations to overload also with F2, F3, etc.

int main() {
    char sep = '\n';

    void BOOST_LOCAL_FUNCTION_PARAMS(const double& item, const bind& sep) {
        std::cout << item << sep;
    } BOOST_LOCAL_FUNCTION_NAME(print_double)

    void BOOST_LOCAL_FUNCTION_PARAMS(const std::string& item, const bind& sep) {
        std::cout << item << sep;
    } BOOST_LOCAL_FUNCTION_NAME(print_string)

    boost::function< void (const double&) > pd(print_double);
    boost::function< void (const std::string&) > ps(print_string);

    boost::local::function::overload<
          boost::function< void (const double&) >
        , boost::function< void (const std::string&) >
> print(pd, ps);

    std::vector<double> d(2);
    d[0] = 1.2; d[1] = 3.4;
    std::for_each(d.begin(), d.end(), print);

    std::vector<std::string> s(3);
    s[0] = "ab"; s[1] = "cd"; s[2] = "ef";
    std::for_each(s.begin(), s.end(), ps);

    return 0;
}

E:\sandbox\boost-sandbox-local\libs\local\example>bjam -q overload_va
...patience...
...found 714 targets...
...updating 3 targets...
compile-c-c++ bin\msvc-8.0\debug\threading-multi\overload_va.obj
overload_va.cpp
e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(601) : error
 C2594: 'argument' : ambiguous conversions from 'boost::local::function::overloa
d<F0,F1> *' to 'const boost::function_base *'
        with
        [
            F0=boost::function<void (const double &)>,
            F1=boost::function<void (const std::string &)>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(492)
 : see reference to function template instantiation 'bool boost::detail::functio
n::basic_vtable1<R,T0>::assign_to<F>(FunctionObj,boost::detail::function::functi
on_buffer &,boost::detail::function::function_obj_tag)' being compiled
        with
        [
            R=void,
            T0=const double &,
            F=boost::local::function::overload<boost::function<void (const doubl
e &)>,boost::function<void (const std::string &)>>,
            FunctionObj=boost::local::function::overload<boost::function<void (c
onst double &)>,boost::function<void (const std::string &)>>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(909)
 : see reference to function template instantiation 'bool boost::detail::functio
n::basic_vtable1<R,T0>::assign_to<Functor>(F,boost::detail::function::function_b
uffer &)' being compiled
        with
        [
            R=void,
            T0=const double &,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>,
            F=boost::local::function::overload<boost::function<void (const doubl
e &)>,boost::function<void (const std::string &)>>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(722)
 : see reference to function template instantiation 'void boost::function1<R,T0>
::assign_to<Functor>(Functor)' being compiled
        with
        [
            R=void,
            T0=const double &,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(1043
) : see reference to function template instantiation 'boost::function1<R,T0>::fu
nction1<Functor>(Functor,int)' being compiled
        with
        [
            R=void,
            T0=const double &,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>
        ]
        overload_va.cpp(57) : see reference to function template instantiation '
boost::function<Signature>::function<boost::local::function::overload<F0,F1>>(Fu
nctor,int)' being compiled
        with
        [
            Signature=void (const double &),
            F0=boost::function<void (const double &)>,
            F1=boost::function<void (const std::string &)>,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>
        ]
e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(601) : error
 C2594: 'argument' : ambiguous conversions from 'boost::local::function::overloa
d<F0,F1> *' to 'const boost::function_base *'
        with
        [
            F0=boost::function<void (const double &)>,
            F1=boost::function<void (const std::string &)>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(492)
 : see reference to function template instantiation 'bool boost::detail::functio
n::basic_vtable1<R,T0>::assign_to<F>(FunctionObj,boost::detail::function::functi
on_buffer &,boost::detail::function::function_obj_tag)' being compiled
        with
        [
            R=void,
            T0=const std::basic_string<char,std::char_traits<char>,std::allocato
r<char>> &,
            F=boost::local::function::overload<boost::function<void (const doubl
e &)>,boost::function<void (const std::string &)>>,
            FunctionObj=boost::local::function::overload<boost::function<void (c
onst double &)>,boost::function<void (const std::string &)>>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(909)
 : see reference to function template instantiation 'bool boost::detail::functio
n::basic_vtable1<R,T0>::assign_to<Functor>(F,boost::detail::function::function_b
uffer &)' being compiled
        with
        [
            R=void,
            T0=const std::basic_string<char,std::char_traits<char>,std::allocato
r<char>> &,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>,
            F=boost::local::function::overload<boost::function<void (const doubl
e &)>,boost::function<void (const std::string &)>>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(722)
 : see reference to function template instantiation 'void boost::function1<R,T0>
::assign_to<Functor>(Functor)' being compiled
        with
        [
            R=void,
            T0=const std::basic_string<char,std::char_traits<char>,std::allocato
r<char>> &,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>
        ]
        e:\sandbox\boost-trunk.windows\boost/function/function_template.hpp(1043
) : see reference to function template instantiation 'boost::function1<R,T0>::fu
nction1<Functor>(Functor,int)' being compiled
        with
        [
            R=void,
            T0=const std::basic_string<char,std::char_traits<char>,std::allocato
r<char>> &,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>
        ]
        overload_va.cpp(57) : see reference to function template instantiation '
boost::function<Signature>::function<boost::local::function::overload<F0,F1>>(Fu
nctor,int)' being compiled
        with
        [
            Signature=void (const std::string &),
            F0=boost::function<void (const double &)>,
            F1=boost::function<void (const std::string &)>,
            Functor=boost::local::function::overload<boost::function<void (const
 double &)>,boost::function<void (const std::string &)>>
        ]

    call "C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat" x86 >nul
cl /Zm800 -nologo @"bin\msvc-8.0\debug\threading-multi\overload_va.obj.rsp"

...failed compile-c-c++ bin\msvc-8.0\debug\threading-multi\overload_va.obj...
...failed updating 1 target...

Thanks a lot!

-- 
Lorenzo

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk