Boost type_erasure any with const member function
Asked Answered
S

1

8

I want to access a getter of type-erased types in a vector. The getter is marked const. Somehow, the const-ness does not propagate to the boost::any wrapper object. The minimal example

#include <iostream>
#include <vector>

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

using namespace boost;
using namespace boost::type_erasure;

BOOST_TYPE_ERASURE_MEMBER((has_test), test, 1)

using AnyTest = any<mpl::vector<copy_constructible<>,
                                has_test<int(double)>,
                                relaxed>>;

struct ATest {
  int test(double x) const {
    return 5;
  }
};

int main() {
  auto xs = std::vector<AnyTest>{};
  xs.push_back(ATest{});

  for (const auto& x : xs) {
    std::cout << x.test(42.0) << '\n';
  }
}

results in an error saying

clang++ -O3 -std=c++14    minimal.cc   -o minimal
minimal.cc:28:18: error: member function 'test' not viable: 'this' argument has type 'const
      boost::type_erasure::any<boost::mpl::vector<boost::type_erasure::copy_constructible<boost::type_erasure::_self>, has_test<int (double),
      boost::type_erasure::_self>, boost::type_erasure::relaxed, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
      mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, boost::type_erasure::_self>', but
      function is not marked const
    std::cout << x.test(42.0) << '\n';
                 ^
minimal.cc:11:39: note: 'test' declared here
BOOST_TYPE_ERASURE_MEMBER((has_test), test, 1)
                                      ^
/opt/local/include/boost/type_erasure/member.hpp:133:9: note: expanded from macro 'BOOST_TYPE_ERASURE_MEMBER'
        member,                                                                             \
        ^
/opt/local/include/boost/type_erasure/member.hpp:242:64: note: expanded from macro 'BOOST_TYPE_ERASURE_MEMBER_I'
    BOOST_TYPE_ERASURE_MEMBER_II(namespace_name, concept_name, member, N)
                                                               ^
/opt/local/include/boost/type_erasure/member.hpp:170:44: note: expanded from macro 'BOOST_TYPE_ERASURE_MEMBER_II'
        typename rebind_any<Base, R>::type member(                                              \
                                           ^
1 error generated.
make: *** [minimal] Error 1

However, once const auto& x in the for-loop is changed to just auto& x, it works. What's the reason for this and how can I require the wrapper object to respect constness?

Saenz answered 23/9, 2015 at 15:24 Comment(0)
D
3

You need to specify the const-ness in the concept like this:

using AnyTest = any<mpl::vector<
                    copy_constructible<>, 
                    has_test<int(double), const _self>, relaxed> 
                  >;

Found on the documentation page "Basic Usage"


Live On Coliru

// https://mcmap.net/q/1369880/-boost-type_erasure-any-with-const-member-function
#include <iostream>
#include <vector>

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

using namespace boost;
using namespace boost::type_erasure;

BOOST_TYPE_ERASURE_MEMBER((has_test), test, 1)

using AnyTest = any<mpl::vector<
                    copy_constructible<>, 
                    has_test<int(double), const _self>, relaxed> 
                  >;

struct ATest {
    int test(double) const { return 5; }
};

int main() {
    auto xs = std::vector<AnyTest>{};
    xs.push_back(ATest{});

    for (auto const &x : xs) {
        std::cout << x.test(42.0) << '\n';
    }
}

Prints

5
Daybreak answered 23/9, 2015 at 20:44 Comment(2)
I was sure I had tried it this way. Probably put the const _self outside of the vector. Thanks a lot!Saenz
@KevinDungs you should have mentioned it in the question :) I wouldn't want to admit how much time I wasted scavenging through the source before I ran into that bit in the docsDaybreak

© 2022 - 2024 — McMap. All rights reserved.