键入擦除类型擦除,“任何”问题
Type erasing type erasure, `any` questions?
所以,假设我想使用类型擦除来键入擦除。
我可以为变体创建伪方法,以实现自然:
pseudo_method print = [](auto&& self, auto&& os){ os << self; };
std::variant<A,B,C> var = // create a variant of type A B or C
(var->*print)(std::cout); // print it out without knowing what it is
我的问题是,我如何将其扩展到std::any
?
它不能"原始"完成。 但是在我们分配/构造std::any
时,我们就有了我们需要的类型信息。
因此,从理论上讲,增强any
:
template<class...OperationsToTypeErase>
struct super_any {
std::any data;
// or some transformation of OperationsToTypeErase?
std::tuple<OperationsToTypeErase...> operations;
// ?? what for ctor/assign/etc?
};
可以以某种方式自动重新绑定一些代码,以便上述类型的语法可以工作。
理想情况下,它将与变体情况一样简洁。
template<class...Ops, class Op,
// SFINAE filter that an op matches:
std::enable_if_t< std::disjunction< std::is_same<Ops, Op>... >{}, int>* =nullptr
>
decltype(auto) operator->*( super_any<Ops...>& a, any_method<Op> ) {
return std::get<Op>(a.operations)(a.data);
}
现在我可以将其保留为一种类型,但合理地使用 lambda 语法来保持简单吗?
理想情况下,我想要:
any_method<void(std::ostream&)> print =
[](auto&& self, auto&& os){ os << self; };
using printable_any = make_super_any<&print>;
printable_any bob = 7; // sets up the printing data attached to the any
int main() {
(bob->*print)(std::cout); // prints 7
bob = 3.14159;
(bob->*print)(std::cout); // prints 3.14159
}
或类似的语法。 这不可能吗? 不可行? 容易?
这是一个使用 C++14 和 boost::any
的解决方案,因为我没有 C++17 编译器。
我们最终得到的语法是:
const auto print =
make_any_method<void(std::ostream&)>([](auto&& p, std::ostream& t){ t << p << "n"; });
super_any<decltype(print)> a = 7;
(a->*print)(std::cout);
这几乎是最佳的。 我认为简单的 C++17 更改,它应该看起来像:
constexpr any_method<void(std::ostream&)> print =
[](auto&& p, std::ostream& t){ t << p << "n"; };
super_any<&print> a = 7;
(a->*print)(std::cout);
在 C++17 中,我会通过auto*...
指向any_method
的指针而不是decltype
噪音来改进这一点。
公开继承any
有点风险,好像有人把any
从上面拿下来修改,any_method_data
tuple
就会过时。 也许我们应该模仿整个any
界面,而不是公开继承。
@dyp在给OP的评论中写了一个概念证明。 这是基于他的工作,并添加了价值语义(从boost::any
窃取(。 @cpplearner 的基于指针的解决方案用于缩短它(谢谢!(,然后我在此基础上添加了 vtable 优化。
首先,我们使用标签来传递类型:
template<class T>struct tag_t{constexpr tag_t(){};};
template<class T>constexpr tag_t<T> tag{};
这个 trait 类获取与any_method
一起存储的签名:
这将创建一个函数指针类型,以及所述函数指针的工厂,给定一个any_method
:
template<class any_method, class Sig=any_sig_from_method<any_method>>
struct any_method_function;
template<class any_method, class R, class...Args>
struct any_method_function<any_method, R(Args...)>
{
using type = R(*)(boost::any&, any_method const*, Args...);
template<class T>
type operator()( tag_t<T> )const{
return [](boost::any& self, any_method const* method, Args...args) {
return (*method)( boost::any_cast<T&>(self), decltype(args)(args)... );
};
}
};
现在我们不想在我们的super_any
中为每个操作存储一个函数指针。 因此,我们将函数指针捆绑到一个 vtable 中:
template<class...any_methods>
using any_method_tuple = std::tuple< typename any_method_function<any_methods>::type... >;
template<class...any_methods, class T>
any_method_tuple<any_methods...> make_vtable( tag_t<T> ) {
return std::make_tuple(
any_method_function<any_methods>{}(tag<T>)...
);
}
template<class...methods>
struct any_methods {
private:
any_method_tuple<methods...> const* vtable = 0;
template<class T>
static any_method_tuple<methods...> const* get_vtable( tag_t<T> ) {
static const auto table = make_vtable<methods...>(tag<T>);
return &table;
}
public:
any_methods() = default;
template<class T>
any_methods( tag_t<T> ): vtable(get_vtable(tag<T>)) {}
any_methods& operator=(any_methods const&)=default;
template<class T>
void change_type( tag_t<T> ={} ) { vtable = get_vtable(tag<T>); }
template<class any_method>
auto get_invoker( tag_t<any_method> ={} ) const {
return std::get<typename any_method_function<any_method>::type>( *vtable );
}
};
我们可以专门针对 vtable 较小(例如,1 个项目(的情况进行专门处理,并在这些情况下使用存储在类中的直接指针以提高效率。
现在我们开始super_any
。 我使用super_any_t
使super_any
的声明更容易一些。
template<class...methods>
struct super_any_t;
这将搜索超级任何支持SFINAE的方法:
template<class super_any, class method>
struct super_method_applies : std::false_type {};
template<class M0, class...Methods, class method>
struct super_method_applies<super_any_t<M0, Methods...>, method> :
std::integral_constant<bool, std::is_same<M0, method>{} || super_method_applies<super_any_t<Methods...>, method>{}>
{};
这是我们全局创建的伪方法指针,print
const
ly。
我们将构建它的对象存储在 any_method
. 请注意,如果您使用非 lambda 构建它,事情可能会变得毛茸茸的,因为此any_method
的类型用作调度机制的一部分。
template<class Sig, class F>
struct any_method {
using signature=Sig;
private:
F f;
public:
template<class Any,
// SFINAE testing that one of the Anys's matches this type:
std::enable_if_t< super_method_applies< std::decay_t<Any>, any_method >{}, int>* =nullptr
>
friend auto operator->*( Any&& self, any_method const& m ) {
// we don't use the value of the any_method, because each any_method has
// a unique type (!) and we check that one of the auto*'s in the super_any
// already has a pointer to us. We then dispatch to the corresponding
// any_method_data...
return [&self, invoke = self.get_invoker(tag<any_method>), m](auto&&...args)->decltype(auto)
{
return invoke( decltype(self)(self), &m, decltype(args)(args)... );
};
}
any_method( F fin ):f(std::move(fin)) {}
template<class...Args>
decltype(auto) operator()(Args&&...args)const {
return f(std::forward<Args>(args)...);
}
};
我相信C++17 中不需要的工厂方法:
template<class Sig, class F>
any_method<Sig, std::decay_t<F>>
make_any_method( F&& f ) {
return {std::forward<F>(f)};
}
这是增强any
. 它既是一个any
,又带有一堆类型擦除函数指针,只要包含的any
会发生变化:
template<class... methods>
struct super_any_t:boost::any, any_methods<methods...> {
private:
template<class T>
T* get() { return boost::any_cast<T*>(this); }
public:
template<class T,
std::enable_if_t< !std::is_same<std::decay_t<T>, super_any_t>{}, int>* =nullptr
>
super_any_t( T&& t ):
boost::any( std::forward<T>(t) )
{
using dT=std::decay_t<T>;
this->change_type( tag<dT> );
}
super_any_t()=default;
super_any_t(super_any_t&&)=default;
super_any_t(super_any_t const&)=default;
super_any_t& operator=(super_any_t&&)=default;
super_any_t& operator=(super_any_t const&)=default;
template<class T,
std::enable_if_t< !std::is_same<std::decay_t<T>, super_any_t>{}, int>* =nullptr
>
super_any_t& operator=( T&& t ) {
((boost::any&)*this) = std::forward<T>(t);
using dT=std::decay_t<T>;
this->change_type( tag<dT> );
return *this;
}
};
由于我们将any_method
存储为const
对象,因此使super_any
更容易一些:
template<class...Ts>
using super_any = super_any_t< std::remove_const_t<std::remove_reference_t<Ts>>... >;
测试代码:
const auto print = make_any_method<void(std::ostream&)>([](auto&& p, std::ostream& t){ t << p << "n"; });
const auto wprint = make_any_method<void(std::wostream&)>([](auto&& p, std::wostream& os ){ os << p << L"n"; });
const auto wont_work = make_any_method<void(std::ostream&)>([](auto&& p, std::ostream& t){ t << p << "n"; });
struct X {};
int main()
{
super_any<decltype(print), decltype(wprint)> a = 7;
super_any<decltype(print), decltype(wprint)> a2 = 7;
(a->*print)(std::cout);
(a->*wprint)(std::wcout);
// (a->*wont_work)(std::cout);
double d = 4.2;
a = d;
(a->*print)(std::cout);
(a->*wprint)(std::wcout);
(a2->*print)(std::cout);
(a2->*wprint)(std::wcout);
// a = X{}; // generates an error if you try to store a non-printable
}
活生生的例子。
当我尝试在super_any
内存储不可打印的struct X{};
时,错误消息至少在叮当声中似乎是合理的:
main.cpp:150:87: error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'X') const auto x0 = make_any_method<void(std::ostream&)>([](auto&& p, std::ostream& t){ t << p << "n"; });
当您尝试将X{}
分配到super_any<decltype(x0)>
时,就会发生这种情况。
any_method
的结构与pseudo_method
充分兼容,的作用类似于变体,它们可能会被合并。
我在这里使用手动 vtable 将类型擦除开销保持在每super_any
1 个指针。 这会增加每个any_method调用的重定向成本。 我们可以很容易地将指针直接存储在super_any
中,并且将其作为super_any
参数并不难。 无论如何,在 1 擦除方法的情况下,我们应该直接存储它。
两个相同类型的不同any_method
(例如,都包含函数指针(生成相同类型的super_any
。 这会导致查找时出现问题。
区分它们有点棘手。 如果我们更改super_any
取auto* any_method
,我们可以将所有相同类型的any_method
捆绑在 vtable 元组中,如果指针超过 1,则对匹配的指针进行线性搜索。 编译器应该优化线性搜索,除非您正在做一些疯狂的事情,例如将引用或指针传递给我们正在使用的特定any_method
。
然而,这似乎超出了这个答案的范围;这种改进的存在就足够了。
此外,可以添加一个在左侧获取指针(甚至引用!(的->*
,让它检测到这一点并将其传递给 lambda。 这可以使其成为真正的"任何方法",因为它可以使用该方法处理变体、super_anys和指针。
通过一些if constexpr
工作,lambda 可以在每种情况下分支执行 ADL 或方法调用。
这应该给我们:
(7->*print)(std::cout);
((super_any<&print>)(7)->*print)(std::cout); // C++17 version of above syntax
((std::variant<int, double>{7})->*print)(std::cout);
int* ptr = new int(7);
(ptr->*print)(std::cout);
(std::make_unique<int>(7)->*print)(std::cout);
(std::make_shared<int>(7)->*print)(std::cout);
any_method
只是"做正确的事"(即将价值提供给std::cout <<
(。
这是我的解决方案。它看起来比Yakk的短,并且不使用std::aligned_storage
和放置新。它还支持有状态和局部函子(这意味着可能永远不可能写super_any<&print>
,因为print
可能是局部变量(。
any_method:
template<class F, class Sig> struct any_method;
template<class F, class Ret, class... Args> struct any_method<F,Ret(Args...)> {
F f;
template<class T>
static Ret invoker(any_method& self, boost::any& data, Args... args) {
return self.f(boost::any_cast<T&>(data), std::forward<Args>(args)...);
}
using invoker_type = Ret (any_method&, boost::any&, Args...);
};
make_any_method:
template<class Sig, class F>
any_method<std::decay_t<F>,Sig> make_any_method(F&& f) {
return { std::forward<F>(f) };
}
super_any:
template<class...OperationsToTypeErase>
struct super_any {
boost::any data;
std::tuple<typename OperationsToTypeErase::invoker_type*...> operations = {};
template<class T, class ContainedType = std::decay_t<T>>
super_any(T&& t)
: data(std::forward<T>(t))
, operations((OperationsToTypeErase::template invoker<ContainedType>)...)
{}
template<class T, class ContainedType = std::decay_t<T>>
super_any& operator=(T&& t) {
data = std::forward<T>(t);
operations = { (OperationsToTypeErase::template invoker<ContainedType>)... };
return *this;
}
};
运算符->*:
template<class...Ops, class F, class Sig,
// SFINAE filter that an op matches:
std::enable_if_t< std::disjunction< std::is_same<Ops, any_method<F,Sig>>... >{}, int> = 0
>
auto operator->*( super_any<Ops...>& a, any_method<F,Sig> f) {
auto fptr = std::get<typename any_method<F,Sig>::invoker_type*>(a.operations);
return [fptr,f, &a](auto&&... args) mutable {
return fptr(f, a.data, std::forward<decltype(args)>(args)...);
};
}
用法:
#include <iostream>
auto print = make_any_method<void(std::ostream&)>(
[](auto&& self, auto&& os){ os << self; }
);
using printable_any = super_any<decltype(print)>;
printable_any bob = 7; // sets up the printing data attached to the any
int main() {
(bob->*print)(std::cout); // prints 7
bob = 3.14159;
(bob->*print)(std::cout); // prints 3.14159
}
住
- 引用一个已擦除类型(void*)的指针
- 擦除while循环中迭代的元素
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- C++擦除(如果存在)
- 在映射擦除c++期间执行循环的次数
- 为什么擦除方法会影响结束方法
- C++ 字符串类擦除成员函数的时空复杂性
- 类型擦除的std::function与虚拟函数调用的开销
- C++14 中unordered_map矢量和擦除删除成语的奇怪行为
- C++ 擦除函数中需要澄清
- 循环挂起迭代的 std::擦除 on std::list
- 擦除许多矢量元素,同时使用'auto'
- 如何擦除冗余输入?
- C++ STL 设置按值擦除
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- 如何在写入文件时擦除最后一个逗号
- std::list c++是连续的,那么在序列中的任何位置插入和擦除操作都需要恒定的时间
- "std::map with mutexes"与并行插入、查找、擦除之间是否有任何速度测试"libcds maps (Michael Hashmap and Split Order List)"?
- 键入擦除类型擦除,“任何”问题