可以使用数组大小​​函数的结果作为数组大小吗?

发布于 2024-09-26 01:08:00 字数 394 浏览 2 评论 0原文

// sizeofarray.cpp
#include <iostream>
template <typename T,int N>
int size(T (&Array)[N])
{
  return N;
}

int main()
{
   char p[]="Je suis trop bon, et vous?";
   char q[size(p)]; // (A)
   return 0;
}

我听说C++中的数组大小必须是常量表达式。所以 char q[size(p)] 无效,对吗?但当我尝试时没有出现任何错误,

 g++ -Wall sizeofarray.cpp

为什么?

// sizeofarray.cpp
#include <iostream>
template <typename T,int N>
int size(T (&Array)[N])
{
  return N;
}

int main()
{
   char p[]="Je suis trop bon, et vous?";
   char q[size(p)]; // (A)
   return 0;
}

I heard that an array size in C++ must be a constant expression. So char q[size(p)] is invalid, am I right? But I got no errors when I tried

 g++ -Wall sizeofarray.cpp

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声情话 2024-10-03 01:08:01

我不敢苟同这里的所有答案。代码显示完全没问题,除了一个小问题(这绝对不是 VLA),

template <typename T,int N> 
int size(T (&Array)[N]) 
{ 
  return N; 
} 

int main() 
{ 
   char p[]="Je suis trop bon, et vous?"; 
   char q[sizeof(p)]; // (A), not sizeof and not size as in OP
   return 0; 
} 

我想知道 sizeof 的结果始终是 const 值,因此代码应该没问题。

上面的代码在 VS 2010 和 Comeau(严格模式)上构建良好

$5.3.3/6-“结果是一个常数
输入 size_t。 [注:size_t已定义
在标准头(18.1)中。”

I beg to differ with all the answers here. The code show is perfectly fine except for a minor issue (which is definitely not VLA)

template <typename T,int N> 
int size(T (&Array)[N]) 
{ 
  return N; 
} 

int main() 
{ 
   char p[]="Je suis trop bon, et vous?"; 
   char q[sizeof(p)]; // (A), not sizeof and not size as in OP
   return 0; 
} 

I was wondering that the result of the sizeof is always a const value, and hence the code should be fine.

The above code builds fine on VS 2010 and Comeau(strict mode)

$5.3.3/6- "The result is a constant of
type size_t. [Note: size_t is defined
in the standard header (18.1)."

醉生梦死 2024-10-03 01:08:01

我使用 g++ 4.4.3 并具有以下别名,这样我就永远不会忘记打开警告:

$ alias g++
alias g++='g++ -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings'

如果使用上述编译,则会出现一些警告。以下步骤显示不同的选项如何显示不同的警告。

不带警告选项的编译不会显示任何警告

$ \g++ sizeofarray.cpp 

打开 -Wall

$ \g++ -Wall sizeofarray.cpp
sizeofarray.cpp: In function ‘int main()’:
sizeofarray.cpp:12: warning: unused variable ‘q’

打开 -Wextra

$ \g++ -Wall -Wextra sizeofarray.cpp 
sizeofarray.cpp: In function ‘int main()’:
sizeofarray.cpp:12: warning: unused variable ‘q’
sizeofarray.cpp: At global scope:
sizeofarray.cpp: In instantiation of ‘int size(T (&)[N]) [with T = char, int N = 27]’:
sizeofarray.cpp:12:   instantiated from here
sizeofarray.cpp:4: warning: unused parameter ‘Array’

最后打开 -pedantic 来捕获真正的问题

$ \g++ -Wall -Wextra -pedantic  sizeofarray.cpp 
sizeofarray.cpp: In function ‘int main()’:
sizeofarray.cpp:12: warning: ISO C++ forbids variable length array ‘q’
sizeofarray.cpp:12: warning: unused variable ‘q’
sizeofarray.cpp: At global scope:
sizeofarray.cpp: In instantiation of ‘int size(T (&)[N]) [with T = char, int N = 27]’:
sizeofarray.cpp:12:   instantiated from here
sizeofarray.cpp:4: warning: unused parameter ‘Array’

I use g++ 4.4.3 and have the following alias so that I never forget to turn on the warnings:

$ alias g++
alias g++='g++ -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings'

If compiled with the above, there would be some warnings. Following steps show how different options show different warnings.

Compilation with no warning option does not show any warning

$ \g++ sizeofarray.cpp 

Turning on -Wall

$ \g++ -Wall sizeofarray.cpp
sizeofarray.cpp: In function ‘int main()’:
sizeofarray.cpp:12: warning: unused variable ‘q’

Turning on -Wextra

$ \g++ -Wall -Wextra sizeofarray.cpp 
sizeofarray.cpp: In function ‘int main()’:
sizeofarray.cpp:12: warning: unused variable ‘q’
sizeofarray.cpp: At global scope:
sizeofarray.cpp: In instantiation of ‘int size(T (&)[N]) [with T = char, int N = 27]’:
sizeofarray.cpp:12:   instantiated from here
sizeofarray.cpp:4: warning: unused parameter ‘Array’

Finally turning on -pedantic to catch the real problem

$ \g++ -Wall -Wextra -pedantic  sizeofarray.cpp 
sizeofarray.cpp: In function ‘int main()’:
sizeofarray.cpp:12: warning: ISO C++ forbids variable length array ‘q’
sizeofarray.cpp:12: warning: unused variable ‘q’
sizeofarray.cpp: At global scope:
sizeofarray.cpp: In instantiation of ‘int size(T (&)[N]) [with T = char, int N = 27]’:
sizeofarray.cpp:12:   instantiated from here
sizeofarray.cpp:4: warning: unused parameter ‘Array’
寻找我们的幸福 2024-10-03 01:08:00

我听说C++中的数组大小必须是常量表达式。

正确的

所以 char q[size(p)] 无效,对吗?

根据 ISO C++,是的!

但是我尝试时没有遇到错误

g++ -Wall sizeofarray.cpp

这是因为 g++ 支持 VLA (Variable Length数组)作为扩展。

C++0x中有 constexpr 功能,借助该功能

constexpr int size(T (&Array)[N])
{
  return N;
}

,您可以编写 char q[size(p)] 是合法的。

编辑:另请阅读此内容 文章 [博客等等]

I heard that an array size in C++ must be a constant expression.

Correct

So char q[size(p)] is invalid, am I right?

According to ISO C++, yes!

But I got no errors when I tried

g++ -Wall sizeofarray.cpp

That's because g++ supports VLA (Variable Length Array) as an extension.

In C++0x there is constexpr feature with the help of which you can write

constexpr int size(T (&Array)[N])
{
  return N;
}

and then char q[size(p)] would be legal.

EDIT : Also read this article [blog whatever]

姜生凉生 2024-10-03 01:08:00

就像 Prasoon 说的,它不是一个常数表达。现在,您可以获得数组大小的常量表达式值,如下

template <std::size_t N>
struct type_of_size
{
    typedef char type[N];
};

template <typename T, std::size_t Size>
typename type_of_size<Size>::type& sizeof_array_helper(T(&)[Size]);

#define sizeof_array(pArray) sizeof(sizeof_array_helper(pArray))

所示 :模板功能工作关闭">此处。您基本上将数组的大小编码为类型的大小,然后获取该类型的 sizeof ,给出:

char q[sizeof_array(p)];

Like Prasoon says, it's not a constant expression. For now, you can get a constant-expression value of the size of an array like this:

template <std::size_t N>
struct type_of_size
{
    typedef char type[N];
};

template <typename T, std::size_t Size>
typename type_of_size<Size>::type& sizeof_array_helper(T(&)[Size]);

#define sizeof_array(pArray) sizeof(sizeof_array_helper(pArray))

Explanation here. You basically encode the size of the array into the size of a type, then get the sizeof of that type, giving you:

char q[sizeof_array(p)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文