English 中文(简体)
部队C++ 初步设计者,需要准确的阵列项目数目
原标题:Force C++ initialiser to require exact number of array items
  • 时间:2023-07-24 02:34:47
  •  标签:
  • c++

考虑以下法典:

struct Test
{
   char name[ 9 ];
};

Test gv_Test[ ] = {
   { "a" },
   { "12345678" },
   { "longstri" }
};

该法典将汇编罚款。 如果我把“长篇”改为“长篇”,那么我会发现一个汇编错误,因为我已经夸大了阵列。

我的问题是:如果我不提供每个阵列的tes/char的确切数目,能否让汇编者犯错误? 换言之,“流入”方面的错误,如“a”类?

对我来说,作为一种编辑时间检查,确保上述例子中的每一个“姓名”准确为8个果园和确定器。

问题回答

最为接近的我可以想做的是执行这一条。 测试的初始范围包括一系列大小 9.9 您可以通过提供<条码>来做到这一点。 测试 系指在参考时使用<代码>const char[9]的构件。 在这样做时,只能用大体尺寸8的字面来构造物体。

Here is an example of this:

struct Test
{
    Test(const char (&str)[9]) { std::copy(str, str + 9, name); }
    char name[ 9 ];
};

Test gv_Test[ ] = {
    { "a" },
    { "12345678" },
    { "longstri0" }
};

int main()
{
    
}

http://coliru.stacked-crooked.com/a/2401b20feb3f03ee'rel=“nofollow noreferer” 页: 1 是

main.cpp:29:1: error: could not convert  {"a"}  from  <brace-enclosed initializer list>  to  Test 
   29 | };
      | ^
      | |
      | <brace-enclosed initializer list>
main.cpp:29:1: error: could not convert  {"longstri0"}  from  <brace-enclosed initializer list>  to  Test 
   29 | };
      | ^
      | |
      | <brace-enclosed initializer list>

显示字面太小或大,会产生错误,但<代码>{“12345678”}汇编了罚款。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签