存储字符串文字的数组,没有大小 - 阵列大小的简单模板
我正在探索用于存储文字阵列(任何类型)的方法,而无需侧尺尺寸信息。我绘制了一些方法,但是每个方法都有一些挥之不去的问题。这是最简单的方法,是阵列大小的模板。请注意,这是一个简单的测试用例,实际类可能具有其他成员,其他模板参数和必须提供的模板参数。
template <size_t I>
struct A0 {
std::array<const char*, I> a;
};
问题在于,大小与字符串文字分离,使这类错误过于容易:
// error, not caught
auto a0 = A0<3>{{"1","2"}};
虽然以下Intialization是有效的,因为C ++ 20不允许部分CTAD,如果任何模板参数参数必须必须< /em>被指定。它还将样板交易为样板:'&lt; 3&gt;'换取“ std :: array'。如果不指定类型CTAD将无法推导模板大小参数“ i”。
auto a0 = A0{std::array{"1","2"}};
是否有扣除指南允许:
auto a0 = A0{{"1","2"}};
I am exploring methods for storing arrays of literals (any type) without sidecar size information. I've sketched some approaches but for each have some lingering questions. This is the simplest method, a template over array size. Please note that this is a simple test case and that actual classes might have additional members, additional template parameters, and template parameters that must be provided.
template <size_t I>
struct A0 {
std::array<const char*, I> a;
};
The problem is that the size is detached from the string literals, making these sort of errors too easy:
// error, not caught
auto a0 = A0<3>{{"1","2"}};
While the following intialization is valid, because C++20 does not allow partial CTAD it cannot work if any template argument must be specified. Also it trades boilerplate for boilerplate: '<3>' in exchange for 'std::array'. Without specifying the type CTAD won't be able to deduce the template size parameter 'I'.
auto a0 = A0{std::array{"1","2"}};
Is there a deduction guide that allows:
auto a0 = A0{{"1","2"}};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个模板构造函数,该模板构造函数与用户定义的扣除指南结合使用,可以推论正确的参数计数或导致编译器错误,如果用户指定了不正确的参数数:
You could create a template constructor that that combined with a user defined deduction guidline either deduces the correct argument count or results in a compiler error, if the user specifies the incorrect number of parameters:
因此,您是在询问
auto a0 = a0 {{“ 1”,“ 2”}};
,但首先是auto A0 = a0 = a0 = a0 {“ 1”, “ 2”};
:我认为这很清楚。它只是计算给出的参数数量。
对于
auto a0 = a0 {{“ 1”,“ 2”}};
:这取决于以下事实:可以通过模板参数扣除将支撑的参数推导为阵列类型。这里的警告是,支撑参数的所有元素都必须具有相同的类型。
如果要使用两个扣除指南,则应在第二个中限制
t
不为const char
以确保特殊情况a0 { “ 123”}
不被视为数组情况。您可能需要根据要以此形式允许的隐式转换来限制类型。然后,在类模板参数扣除之后,这两个都依靠聚合初始化(可以遗漏其他括号)来实际构建类实例。但是,如果您在班上有构造函数,则它将工作相同。
So, you are asking about
auto a0 = A0{{"1","2"}};
, but first here is a deduction guide forauto a0 = A0{"1","2"};
:I think it is clear what it does. It simply counts the number of arguments given.
For
auto a0 = A0{{"1","2"}};
:This relies on the fact that a braced argument can be deduced to an array type by template argument deduction. The caveat here is that all the elements of the braced argument must have the same type.
If you want to use both of the deduction guides, you should constrain
T
in the second one not to beconst char
to make sure that the special caseA0{"123"}
is not considered as array case. You might want to constrain the types anyway depending on what implicit conversions you want to allow in this form.Both of these then, after the class template argument deduction, rely on aggregate initialization (the additional braces can be left out) to actually construct the class instance. But if you have constructors in the class instead, it will work the same.