gpt4 book ai didi

c++ - 异构可变参数非类型模板参数计数灵活吗?

转载 作者:行者123 更新时间:2023-11-28 03:27:41 26 4
gpt4 key购买 nike

下面定义了一个可变的非类型嵌套类模板,DEF。根据为 Ts 提供的类型参数,非类型模板参数可能是异构的。

template <typename ...Ts>
struct ABC {
template <Ts ...Xs>
struct DEF {};
};

DEF 对象可以声明如下:

ABC<int,bool>::DEF<17,true> x;

我的问题是,提供给DEF的非类型模板参数的数量是否可以少于提供给ABC的类型模板参数的数量?例如,这些声明是否有效:

ABC<int,bool>::DEF<17> y;
ABC<int,bool>::DEF< > z;

最佳答案

不,因为Ts...是一个包扩展。

§14.5.3 [temp.variadic]

p4 A pack expansion consists of a pattern and an ellipsis, the instantiation of which produces zero or more instantiations of the pattern in a list (described below). The form of the pattern depends on the context in which the expansion occurs. Pack expansions can occur in the following contexts:

  • [...]
  • In a template parameter pack that is a pack expansion (14.1):
    • if the template parameter pack is a parameter-declaration; the pattern is the parameter-declaration without the ellipsis;
    • [...]

p6 The instantiation of a pack expansion [...] produces a list E1, E2, ..., EN, where N is the number of elements in the pack expansion parameters. [...]

所以你的两个例子都是错误的,因为 DEF正好需要 <int, bool> .

关于c++ - 异构可变参数非类型模板参数计数灵活吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13503877/

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