【发布时间】:2021-01-30 13:58:02
【问题描述】:
有时我想通过一个真正的类更改我通过使用定义的类型。
例如,我这里有一个示例,我如何可以像使用类型一样使用结构:
using t_int_pair = std::pair< int, int >;
struct s_int_pair : public std::pair< int, int >
{
using std::pair< int, int >::pair; // inherit ctor
};
void foo()
{
auto [a1, a2] = t_int_pair{ 0, 0 };
auto [b1, b2] = s_int_pair{ 0, 0 };
// ...
}
这很好用。
但是:出于任何原因,相同的代码不适用于 std::tuple,即这会产生编译错误:
using t_int_triple = std::tuple< int, int, int >;
struct s_int_triple : public std::tuple< int, int, int >
{
using std::tuple< int, int, int >::tuple; // inherit ctor
};
void bar()
{
auto [a1, a2, a3] = t_int_triple{ 0, 0, 0 };
auto [b1, b2, b3] = s_int_triple{ 0, 0, 0 }; // ERROR: cannot decompose class type 'std::_Tuple_impl<0, int, int>'
// ...
}
有谁知道,为什么会这样?
有没有办法解决这个问题?
我在 Compiler Explorer 上使用 clang、gcc 和 msvc 对此进行了测试。
感谢您的帮助,
问候,
Zoppo
【问题讨论】:
-
不,初始化工作正常,失败的是结构化绑定。
-
我很惊讶从标准对继承允许结构化分解工作。也许是公众成员?这可能就是原因。
标签: c++