If I have a struct defined such as the following: struct blank { int : 0; }; Will the compiler optimise this away to nothing at runtime? I ask because of this rather popular SO question. I would like to employ similar compile-time checks in my own C/C++ code. I'm trying to make the program as optimal as possible, so I don't want these empty structs hanging around at runtime if I
如果我有一个如下定义的结构: struct blank { int : 0; }; 编译器会在运行时将其优化为什么? 我问,因为这个颇受欢迎的SO问题。 我想在我自己的C / C ++代码中使用类似的编译时检查。 我试图让程序尽可能优化,所以我不希望这些空的结构体在运行时四处闲逛,如果我只是将它们用于编译时检查。 在一个侧面说明,是否有一个C ++ - 惯用的方式来实现链接中的相同结果? 当对象实现基类时,C ++只允许进行优化。
If x was a 4 bit word like 1010, and you did the operation !!x, Wouldn't the first !x return 0101, And the second !(!x) return 1010? Rather it returns ...0001 or ...0000. Why is this? In C, !x is either 1 or 0, So !!x is a "collapse to 0 or 1 operator" in the sense that any non-zero number is mapped to 1, and 0 stays as it is. This can be useful on occasions. In C++, !x
如果x是像1010一样的4位字,并且您执行了操作!! x, 不会第一个!x返回0101, 第二个!(!x)返回1010? 相反,它返回... 0001或... 0000。 为什么是这样? 在C中, !x是1或0,因此!!x是一个“折叠为0或1运算符”,意思是任何非零数字映射为1,并且0保持原样。 这在有些情况下很有用。 在C ++中, !x是一个bool类型,因此!!x是“折叠为假或真运算符”,意思是任何非零数字映射为true ,零映射为false 。 ! 运算符执
This question already has an answer here: Double Negation in C++ code 14 answers What is “!!” in C? [duplicate] 7 answers It is not as simple as double negation. For example, if you have x == 5 , and then apply two ! operators ( !!x ), it will become 1 - so, it is used for normalizing boolean values in {0, 1} range. Note that you can use zero as boolean false, and non-zero for boolean
这个问题在这里已经有了答案: C ++代码中的双重否定14个答案 什么是C中的“!!”? [复制] 7个答案 这不像双重否定那么简单。 例如,如果您有x == 5 ,然后应用两个! 运算符( !!x ),它将变为1 - 因此,它用于标准化{0, 1}范围内的布尔值。 请注意,您可以使用零作为布尔值false,布尔值使用非零值true,但您可能需要将结果标准化为0或1,那就是当!! 是有用的。 它与x != 0 ? 1 : 0相同x != 0 ? 1 : 0 x != 0 ?
The noexcept keyword can be appropriately applied to many function signatures, but I am unsure as to when I should consider using it in practice. Based on what I have read so far, the last-minute addition of noexcept seems to address some important issues that arise when move constructors throw. However, I am still unable to provide satisfactory answers to some practical questions that led me t
noexcept关键字可以适用于许多功能签名,但我不确定何时应该考虑在实践中使用它。 根据我目前阅读的内容, noexcept的最后一刻添加似乎解决了移动构造函数抛出时出现的一些重要问题。 但是,我仍然无法为一些实际问题提供令人满意的答案,这些问题让我首先阅读了更多关于noexcept的内容。 有很多函数的例子,我知道永远不会抛出,但编译器无法自行确定。 在所有这些情况下,我是否应该在函数声明中添加noexcept ? 不得不
As much as I love C and C++, I can't help but scratch my head at the choice of null terminated strings: Length prefixed (ie Pascal) strings existed before C Length prefixed strings make several algorithms faster by allowing constant time length lookup. Length prefixed strings make it more difficult to cause buffer overrun errors. Even on a 32 bit machine, if you allow the string to be
就像我喜欢C和C ++一样,我忍不住在选择以null结尾的字符串时抓住我的头: 在C之前存在长度前缀(即Pascal)字符串 通过允许恒定的时间长度查找,长度前缀字符串使得几种算法更快。 带长前缀的字符串使得更难以导致缓冲区溢出错误。 即使在32位机器上,如果允许字符串为可用内存的大小,则长度前缀字符串只比空字符串宽三个字节。 在16位机器上,这是一个字节。 在64位机器上,4GB是一个合理的字符串长度限制,但即使
Today I discovered alarming behavior when experimenting with bit fields. For the sake of discussion and simplicity, here's an example program: #include <stdio.h> struct Node { int a:16 __attribute__ ((packed)); int b:16 __attribute__ ((packed)); unsigned int c:27 __attribute__ ((packed)); unsigned int d:3 __attribute__ ((packed)); unsigned int e:2 __attribute__ ((packed));
今天,我在试验比特场时发现了令人震惊的行为。 为了讨论和简单起见,下面是一个示例程序: #include <stdio.h> struct Node { int a:16 __attribute__ ((packed)); int b:16 __attribute__ ((packed)); unsigned int c:27 __attribute__ ((packed)); unsigned int d:3 __attribute__ ((packed)); unsigned int e:2 __attribute__ ((packed)); }; int main (int argc, char *argv[]) { Node n; n.a = 12
I have used unions earlier comfortably; today I was alarmed when I read this post and came to know that this code union ARGB { uint32_t colour; struct componentsTag { uint8_t b; uint8_t g; uint8_t r; uint8_t a; } components; } pixel; pixel.colour = 0xff040201; // ARGB::colour is the active member from now on // somewhere down the line, withou
我早些时候很舒服地使用过工会; 今天,当我阅读这篇文章并且知道这段代码时,我感到非常惊慌 union ARGB { uint32_t colour; struct componentsTag { uint8_t b; uint8_t g; uint8_t r; uint8_t a; } components; } pixel; pixel.colour = 0xff040201; // ARGB::colour is the active member from now on // somewhere down the line, without any edit to pixel if(pix
[This question is related to but not the same as this one.] If I try to use values of certain types as boolean expressions, I get a warning. Rather than suppress the warning, I sometimes use the ternary operator ( ?: ) to convert to a bool. Using two not operators ( !! ) seems to do the same thing. Here's what I mean: typedef long T; // similar warning with void * or double T t =
[这个问题和这个问题有关,但不一样。] 如果我尝试使用某些类型的值作为布尔表达式,我会收到警告。 我有时使用三元运算符( ?: :)转换为布尔值,而不是压制警告。 使用两个非运算符( !! )似乎也是这样做的。 这就是我的意思: typedef long T; // similar warning with void * or double T t = 0; bool b = t; // performance warning: forcing 'long' value to 'bool' b = t ? true : false; // ok
I have the following two lines in a header in order to declare a vector containing a template: template <class t> std::vector <t> vec; However I get the following error: data member 'vec' cannot be a member template What did I do wrong? Edit: I don't know that I was understood correctly, I am trying to declare a vector which contains a template, I know that this can
为了声明包含模板的矢量,我在头文件中有以下两行: template <class t> std::vector <t> vec; 但是,我得到以下错误:数据成员'vec'不能成为会员模板我做错了什么? 编辑:我不知道我的理解正确,我试图声明一个包含模板的向量,我知道可以这样做,因为可以具有以下内容: template <class T> void funct(vector <T> v){ } 这个函数接受一个模板的向量作为它的参数,我希望做同样的
Following matrix is given 10001 10100 00000 00000 00111 00100 00100 With one click, the pixel fill tool turns each black pixel into a white one until there isn't a black pixel that can be reached from the previous pixel. A pixel is connected with another one in up to eight ways: north, south, east, west and the four diagonals. Puzzle link is http://www.gild.com/challenges/details/295#
给出以下矩阵 10001 10100 00000 00000 00111 00100 00100 只需点击一次,像素填充工具就会将每个黑色像素变为白色像素,直到没有可以从前一像素到达的黑色像素为止。 一个像素以多达八种方式与另一个像素连接:北,南,东,西和四个对角线。 益智链接是http://www.gild.com/challenges/details/295# 这是我解决它的方式。 任何人都可以告诉我这个问题属于哪个算法类别。 #include <stdio.h> #include <vector&