多重继承中的派生类的行为类似于聚合

Derived class in multiple inheritance behaves like an aggregate

本文关键字:类似于 派生 多重继承      更新时间:2023-10-16

我可以得到以下代码(也可以在cpp首选项 http://en.cppreference.com/w/cpp/utility/variant/visit 中找到(来编译(https://wandbox.org/permlink/stCFKi0VQlF49Bxr(

#include <type_traits>
#include <utility>
template <typename... Types>
struct Overload : public Types... {
using Types::operator()...;
};
template <typename... Types>
auto make_overload(Types&&... instances) {
return Overload<std::decay_t<Types>...>{std::forward<Types>(instances)...};
}
int main() {
auto overloaded = make_overload([](int) {}, [](double) {});
static_cast<void>(overloaded);
}

上面的代码如何在 C++17 中编译? 它不会在 C++14 中编译。 幕后发生了什么? 另外,为什么可变参数using声明在 C++14 中不起作用? 这是哪个新功能?

正如你在parameter_pack中读到的,

C++17 推出

以下是所有允许的上下文的列表:[...]

Using-declarations
在使用声明时,省略号可能会出现在声明符列表中,这在从参数包派生时很有用:

template <typename... bases>
struct X : bases... {
using bases::g...;
};
X<B, D> x; // OK: B::g and D::g introduced