boost fusion 为什么在 c++11 和 c++03 中有不同的结果?

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

为什么下面的类型 as_vet_type 在用 C++03 编译时是 boost::fusion::vector2<const int, const int> 而在用 c++11 编译时是 boost::fusion::vector<int, int>const 在 c++11 中缺失。这是错误还是功能? 我用 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;
}

我收到了某人的评论,但不幸的是它不再可见:( 无论如何,多亏了那个评论,我才知道发生了什么。

原来这个问题与 boost::result_of 有关,而不是 boost::fusion。 当使用 decltype 时,boost::result_of 在 c++11 和 c++03 中的行为可能不同。 boost::result_of 文档在 "Non-class prvalues and cv-qualification" 部分描述了这种差异。

我可以提供这个简化的解释。 在 C++11 中,在这个函数声明中: const int f(); const 被编译器简单地忽略并且 f 签名变为 int f(); 这就是为什么 decltype(const int f());int.

如果声明 const int f();

,GCC 5.3.2 甚至会产生以下警告

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