在名称空间下专门针对类型的访客
我有一个可以包含8种不同类型的变体,其中一些在特定的名称空间下
std::variant<T0, T1, T2, ns::T0, ns::T1, ns::T2> v;
存在一个免费功能,可以使用这些类型的任何类型调用,但是我正在访问这样的变体
int function(T0 t){return 0;}
int function(T1 t){return 1;}
int function(T2 t){return 2;}
namespace ns {
template <typename T>
int function(T t) {return 3;}
}
void doSomething() {
std::visit([](const auto &t) {return function(t);}, v);
}
,但是现在我希望用户能够为了提供自己的访问者,并将其称为,如果提供的访问者与变体类型不匹配,请使用默认的访问者。为此,
int function(T0 t){return 0;}
int function(T1 t){return 1;}
int function(T2 t){return 2;}
namespace ns {
template <typename T>
int function(T t) {return 3;}
}
struct DefaultVisitor
{
template <typename T>
int operator()(T t) const {return function(t);}
}
template <typename VisitorT>
void doSomething() {
std::visit(overloaded{VisitorT(), DefaultVisitor()}, v);
}
如果vistort
提供一个类型的呼叫操作员, 我已经尝试了以下操作不编译。
我如何提供由ns
名称空间中的类型使用的访问者,而不是其他类型的访问者?
I have a variant which can contain 8 different types, some of them under a particular namespace
std::variant<T0, T1, T2, ns::T0, ns::T1, ns::T2> v;
There exists a free function which can be called with any of these types and I am visiting the variant like this
int function(T0 t){return 0;}
int function(T1 t){return 1;}
int function(T2 t){return 2;}
namespace ns {
template <typename T>
int function(T t) {return 3;}
}
void doSomething() {
std::visit([](const auto &t) {return function(t);}, v);
}
However, now I want the user to be able to provide his own visitor and call that, and in case, the provided visitor doesn't match for the variant type, use the default one. To do so I have tried the following
int function(T0 t){return 0;}
int function(T1 t){return 1;}
int function(T2 t){return 2;}
namespace ns {
template <typename T>
int function(T t) {return 3;}
}
struct DefaultVisitor
{
template <typename T>
int operator()(T t) const {return function(t);}
}
template <typename VisitorT>
void doSomething() {
std::visit(overloaded{VisitorT(), DefaultVisitor()}, v);
}
If the VisitorT
provides a call operator for only one type, this works fine, but if it provides a call operator for several types, such as a generic lambda, then it doesn't compile.
How could I provide a VisitorT which is used by the types in the ns
namespace, but not for other types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以调整
Overloaded
具有一些优先级:demo
请确保该类型属于特定的名称空间,但是有一个技巧可能会知道是否允许通过ADL找到函数(因此从
ns
类型继承的类型或> std :: vector&lt; ns :: t0&gt;
将通过该检查)You might adapt
overloaded
to have some priorities:Demo
There is no ways to know for sure that type belongs to a specific namespace, but there is a trick which might be used to know if a type allow to found function via ADL (so types inheriting from
ns
type, orstd::vector<ns::T0>
would pass that check)Demo