c++ - 为什么以下 3 个版本中的 2 个 std::visit 不起作用
问题描述
我试图使用 astd::visit
访问变体中的成员,如果该成员不可用则抛出错误。
我能够得到一个可行的解决方案,但我发现前两次尝试中的错误难以理解。
有谁知道为什么“版本 1”和“版本 2”不起作用?
#include <variant>
#include <vector>
#include <stdexcept>
struct a
{
int value=32;
};
struct b : a
{
};
struct c
{
//empty
};
using t = std::variant<std::monostate,a,b,c>;
struct wacky_visitor{
// Version 1 (doesn't work)
// bool operator()(const auto& l, const auto& r)
// {
// throw std::runtime_error("bad");
// };
// Version 2 (doesn't work)
// template <typename L, typename R>
// bool operator()(const L& l, const R& r)
// {
// throw std::runtime_error("bad");
// };
// Version 3 (works)
template <typename L, typename R>
std::enable_if_t<!(std::is_base_of_v<a, L> && std::is_base_of_v<a, R>), bool> operator()(const L& l, const R& r)
{
throw std::runtime_error("bad");
};
//Shared
template <typename L, typename R>
std::enable_if_t<std::is_base_of_v<a, L> && std::is_base_of_v<a, R>, bool> operator()(const L& l, const R& r)
{
return l.value < r.value;
};
};
int main()
{
std::vector<t> foo_bar = {a(),b()};
const auto comparison = [](const t &lhs, const t &rhs) {
return std::visit(wacky_visitor{}, lhs, rhs);
};
std::sort(foo_bar.begin(), foo_bar.end(), comparison);
return 0;
}
解决方案
你的版本 1 和版本 2 的意思完全一样,所以我只考虑版本 2。
当您调用 时wacky_visitor
,您有两个重载选择:
// first overload
template <typename L, typename R>
bool operator()(L const&, R const&);
// second overload
template <typename L, typename R>
???? operator()(const L& l, const R& r)
????
这个“约束”在哪里enable_if
(我使用引号是因为它是 C++17 可以做的最好的约束,但它不是一个适当的约束,见下文)。在某些情况下,这是一个无效类型,因此将不考虑重载。但是如果它是一个有效的类型,那么……好吧,我们的两个重载是完全一样的。两者在两个论点中都是完全匹配的,没有任何东西可以区分它们。
您的第 3 个版本之所以有效,是因为否定enable_if
条件确保了两个重载中的一个是可行的,因此重载解决方案始终只有一个可供选择的候选者——然后它变得微不足道。
if constexpr
仅使用并具有单个重载更容易:
template <typename L, typename R>
bool operator()(const L& l, const R& r)
{
if constexpr (std::is_base_of_v<a, L> && std::is_base_of_v<a, R>) {
return l.value < r.value;
} else {
throw std::runtime_error("bad");
}
};
在 C++20 中,Concepts 具有附加功能,即受约束的函数模板优于不受约束的函数模板。这意味着你可以这样写:
// first overload as before, whichever syntax
template <typename L, typename R>
bool operator()(L const&, R const&);
// second overload is now constrained
template <typename L, typename R>
requires std::is_base_of_v<a, L> && std::is_base_of_v<a, R>
bool operator()(const L& l, const R& r);
如果第二个重载不可行,则调用第一个重载 - 和以前一样。但是现在,如果第二个重载是可行的,那么无论如何它都可以优先于第一个。
第二个重载也可以这样写:
template <std::derived_from<a> L, std::derived_from<a> R>
bool operator()(const L& l, const R& r);
这意味着大致相同的事情。
推荐阅读
- r - R str_replace_all 在替换字符串中添加括号或括号
- javascript - 在 wkwebview 中更改类的属性
- binary - 将数字编码为 IEEE754 半精度
- regex - 如何检查字段是否包含 Postgresql 中的任何字母?
- arrays - REEXTRACT - 匹配字符串中由特殊字符表示的一个字母
- python - Pandas:使用数据透视表在子图中制作条形图
- visual-studio - I need to undo an action past a refactoring action aka Global Action (*Fix Name Violation)
- r - How to read from REDCap forms with data validation into R (REDCapR::readcap_read)
- julia - time_ns() result not saved by writedlm() in julia
- sql - Oracle PL/SQL how to join first letter of value in a row and another row