具有自由函数const和非常量过载的Boost类型擦除错误

时间:2016-03-19 01:04:20

标签: c++ boost boost-type-erasure

这是代码。

BOOST_TYPE_ERASURE_FREE((has_arg1), arg1, 1)

namespace te = ::boost::type_erasure;

typedef te::any<
            ::boost::mpl::vector<
                te::copy_constructible<>,
                te::typeid_<>,
                te::destructible<>,
                te::assignable<>,
                has_arg1<Vector<Val>&( te::_self& )>,
                has_arg1<const Vector<Val>&( const te::_self& )>>> Bar;

struct Foo {
    Vector<Val> mVals;
};

const Vector<Val>& arg1(const Foo& f) {
    return f.mVals;
}

Vector<Val>& arg1(Foo& f) {
    return f.mVals;
}

BOOST_AUTO_TEST_CASE(Foo1) {
    Foo f;
    Bar i = f;
}

失败,出现以下错误。有什么想法吗?这是不可能使用升压型擦除。我正在使用Clang ++ 3.5编译器。

      functions that differ only in their return type cannot be overloaded
BOOST_TYPE_ERASURE_FREE((has_arg1), arg1, 1)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

1 个答案:

答案 0 :(得分:1)

从技术上讲,问题与Basic Usage

中所述相同
  

如果我们想定义一个const成员函数,我们必须将它改为const _self,如下所示。

所以你要写

<强> Live On Coliru

#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/free.hpp>
#include <boost/type_erasure/member.hpp>
#include <iostream>
#include <vector>

template <typename T>
using Vector = std::vector<T>;

struct Val {};

BOOST_TYPE_ERASURE_FREE((has_free1),     free1,   1)
BOOST_TYPE_ERASURE_MEMBER((has_member1), member1, 0)

namespace te = ::boost::type_erasure;

typedef te::any< ::boost::mpl::vector<te::copy_constructible<>
          , te::typeid_<>
          , te::destructible<>
          , te::assignable<>
          , has_free1<Vector<Val>&(te::_self&)>
          , has_free1<Vector<Val> const&(te::_self const&)>
          , has_member1<Vector<Val>&(), te::_self>
          , has_member1<Vector<Val> const &(), te::_self const> >,
     te::_self const &> Bar;

struct Foo {
    Vector<Val> mVals;
    Vector<Val>&       member1()       { std::cout << __PRETTY_FUNCTION__ << "\n"; return mVals; }
    const Vector<Val>& member1() const { std::cout << __PRETTY_FUNCTION__ << "\n"; return mVals; }
};

Vector<Val>&       free1(Foo& f)       { std::cout << __PRETTY_FUNCTION__ << "\n"; return f.mVals; }
const Vector<Val>& free1(const Foo& f) { std::cout << __PRETTY_FUNCTION__ << "\n"; return f.mVals; }

int main() {
    std::cout << " ==== Foo f; ==============\n";
    {
        Foo f {};
        std::cout << "Direct: "; free1(f);
        std::cout << "Direct: "; f.member1();
        Bar i = f; std::cout << "\n";
        std::cout << "Erased: "; free1(f);
        std::cout << "Erased: "; i.member1();
    }
    std::cout << " ==== Foo const f; ========\n";
    {
        Foo const f {};
        std::cout << "Direct: "; free1(f);
        std::cout << "Direct: "; f.member1();
        Bar i = f; std::cout << "\n";
        std::cout << "Erased: "; free1(f);
        std::cout << "Erased: "; i.member1();
    }
}

打印:

 ==== Foo f; ==============
Direct: Vector<Val>& free1(Foo&)
Direct: Vector<Val>& Foo::member1()

Erased: Vector<Val>& free1(Foo&)
Erased: Vector<Val>& Foo::member1() const
 ==== Foo const f; ========
Direct: Vector<Val>& free1(const Foo&)
Direct: Vector<Val>& Foo::member1() const

Erased: Vector<Val>& free1(const Foo&)
Erased: Vector<Val>& Foo::member1() const