gpt4 book ai didi

c++ - 使用 const 成员函数 boost type_erasure any

转载 作者:可可西里 更新时间:2023-11-01 18:39:01 25 4
gpt4 key购买 nike

我想访问 vector 中类型删除类型的 getter。 getter 被标记为 const。不知何故,const-ness 不会传播到 boost::any 包装器对象。最小的例子

#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';
}
}

导致错误提示

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

但是,一旦将 for 循环中的 const auto& x 更改为 auto& x,它就可以工作了。这是什么原因,我怎样才能要求包装器对象遵守常量性?

最佳答案

你需要像这样在概念中指定const-ness:

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

在文档中找到 page "Basic Usage"


Live On Coliru

// http://stackoverflow.com/questions/32743594/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';
}
}

打印

5

关于c++ - 使用 const 成员函数 boost type_erasure any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com