Search code examples
c++11c++03boost-fusion

boost fusion why there is different result in c++11 and c++03?


Why the following type as_vet_type is boost::fusion::vector2<const int, const int> when compiling with C++03 and boost::fusion::vector<int, int> when compiling with c++11 ? const is missing with c++11. Is this a bug or feature ? I tested this with boost 1.60.

#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/container/vector/vector_fwd.hpp>
#include <boost/fusion/include/vector_fwd.hpp>

#include <boost/fusion/algorithm/transformation/transform.hpp>
#include <boost/fusion/include/transform.hpp>

#include <boost/fusion/container/vector/convert.hpp>
#include <boost/fusion/include/as_vector.hpp>

struct functor
{
    template<class> struct result;

    template<class F, class T>
    struct result<F(T)> {
        typedef const int type;
    };   

    template<class T>
    typename result<functor(T) >::type 
    operator()(T x) const;

};




int main()
{
    typedef boost::fusion::vector<const int & ,char &> cont_type;
    typedef typename boost::fusion::result_of::transform<cont_type ,functor >::type view_type;
    typedef typename boost::fusion::result_of::as_vector<view_type>::type as_vec_type;

    as_vec_type asd;
    asd.x;
    return 0;
}

Solution

  • I got a comment from someone but unfortunetly it is no longer visible :( Anyway thanks to that comment i figured out what is happenning.

    It turns out that this issue is related boost::result_of and not to boost::fusion. boost::result_of can behave diffrently in c++11 when decltype is used and in c++03. boost::result_of documentation describes this diffrence in part "Non-class prvalues and cv-qualification".

    I can provide this simplified explanation. In C++11 , in this function declaration: const int f(); const is simply ignored by compiler and f signature becomes int f(); and thats why decltype(const int f()); is int.

    GCC 5.3.2 will even produce the following warning if you declare const int f();

    prog.cc:5:13: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] const int f()