将boost类型擦除类型转换回原始类型会给我boost :: bad_any_cast

时间:2018-05-09 09:01:04

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

我是新手来提升类型擦除,我遇到了将对象转换回原始类型的问题。根据我对boost文档的理解,我应该能够使用boost :: any_cast将类型擦除的对象转换回原始类型,但是以下代码会因bad_any_cast异常而失败。我究竟做错了什么? 非常感谢!

https://www.boost.org/doc/libs/1_67_0/doc/html/boost/any_cast.html

BOOST_TYPE_ERASURE_MEMBER((has_x), x, 0)

namespace bte = boost::type_erasure;
using xConcept = boost::mpl::vector<has_x <float(), bte::_self> ,
                                                    bte::copy_constructible<>,
                                                    bte::relaxed>;

using AnyXobject = bte::any<xConcept, bte::_self>;

struct xThing{
  float x(){
    return 4.;
  }
  float y(){
    return 5.;
  }
};

int main(){
  // instance of concrete implementation
  xThing i; 
  // using concrete implementation to construct type erased object
  AnyXobject xconc(i); 
  // calling x() correctly prints 4
  std::cout << xconc.x() << std::endl; 

  // converting back to concrete implementation fails with boost::bad_any_cast at runtime
  auto j = boost::any_cast<xThing>(xconc);
  return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要致电boost::type_erasure::any_cast

以下是更正后的计划:

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

BOOST_TYPE_ERASURE_MEMBER((has_x), x, 0)

namespace bte = boost::type_erasure;
using xConcept = boost::mpl::vector<has_x <float(), bte::_self> ,
bte::copy_constructible<>,
bte::relaxed>;

using AnyXobject = bte::any<xConcept, bte::_self>;

struct xThing{
    float x(){
        return 4.;
    }
    float y(){
        return 5.;
    }
};

int main(){
    // instance of concrete implementation
    xThing i;
    // using concrete implementation to construct type erased object
    AnyXobject xconc(i);
    // calling x() correctly prints 4
    std::cout << xconc.x() << std::endl;

    // converting back to concrete implementation fails with boost::bad_any_cast at runtime
    auto j = bte::any_cast<xThing>(xconc);
    return 0;
}