gpt4 book ai didi

C++:使用声明和重载范例

转载 作者:行者123 更新时间:2023-12-04 12:31:36 25 4
gpt4 key购买 nike

我在看 this page关于 C++17 的"new"特性。特别是我几乎理解以下所有代码:

#include <iostream>
#include <variant>

struct Fluid { };
struct LightItem { };
struct HeavyItem { };
struct FragileItem { };

template<class... Ts> struct overload : Ts... { using Ts::operator()...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;

int main() {
std::variant<Fluid, LightItem, HeavyItem, FragileItem> package;

std::visit(overload{
[](Fluid& ) { std::cout << "fluid\n"; },
[](LightItem& ) { std::cout << "light item\n"; },
[](HeavyItem& ) { std::cout << "heavy item\n"; },
[](FragileItem& ) { std::cout << "fragile\n"; }
}, package);
}

明白的是

using Ts::operator()...;

在结构 overload 的定义中。据我所知,这样使用 using 关键字是为了确保 operator()public。但在我看来,就涉及 lambda 而言,它已经公开了。所以我会说这是多余的。我尝试编译和执行

template<class... Ts> struct overload : Ts... { };

一切正常。我错了吗?我错过了什么吗?

最佳答案

使用 目的不仅是为了改变可访问性,而且是为了“解决”歧义和/或取消隐藏基本方法。

https://en.cppreference.com/w/cpp/language/unqualified_lookup#Member_function_definition

otherwise, if the declaration sets in Bi and in C are different, the result is an ambiguous merge: the new lookup set of C has an invalid declaration and a union of the subobjects ealier merged into C and introduced from Bi. This invalid lookup set may not be an error if it is discarded later.

struct Base1
{
void foo() {}
};

struct Base2
{
void foo(int) {}
};

struct D : Base1, Base2
{
// using Base1::foo; // Those would solve the ambiguity
// using Base2::foo;

void bar()
{
foo(); // ambiguous
}
}

struct D2 : Base1
{
// using Base1::foo; // This would unhide Base1::foo

void foo(char) {} // hides Base1::foo

void bar()
{
foo(); // wrong, find D2::foo(char)
}
}

using Ts::operator()...; 就是可变参数语法。

关于C++:使用声明和重载范例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68678971/

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