Assigning a value to a constant syntax or semantic error?

Is the second line of code considered as a syntax error or a semantic error in C++? int a = 7; 3 = a; In standard C++ context-free grammar I found this statement syntactically valid. It is not a syntax error, as the grammar can derive from assignment-expression (5.17) up to integer_literal It is then a semantic error, as stated in 5.17: All require a modifiable lvalue as their left oper

为常量语法或语义错误分配一个值?

第二行代码是否被视为C ++中的语法错误或语义错误? int a = 7; 3 = a; 在标准的C ++上下文无关文法中,我发现这个语句在语法上是有效的。 这不是语法错误,因为语法可以从赋值表达式(5.17)直到integer_literal 这是一个语义错误,如5.17所述: 所有需要一个可修改的左值作为它们的左操作数并返回一个左值指向左操作数。 左值是一个语义概念,而不是句法概念。

Dependent Lookup" (aka ADL, or "Koenig Lookup")?

What are some good explanations on what argument dependent lookup is? Many people also call it Koenig Lookup as well. Preferably I'd like to know: Why is it a good thing? Why is it a bad thing? How does it work? (Note: This is meant to be an entry to Stack Overflow's C++ FAQ.) Koenig Lookup is also commonly known as Argument Dependent Lookup in C++ and most of the Standard C+

依赖查找“(又名ADL或”Koenig Lookup“)?

什么是依赖于参数的查找的一些很好的解释? 许多人也称之为Koenig Lookup。 最好我想知道: 为什么这是件好事? 为什么这是一件坏事? 它是如何工作的? (注意:这是为了进入Stack Overflow的C ++ FAQ。) Koenig Lookup在C ++中也被称为参数依赖查找 ,大多数标准C ++编译器都支持它。 C ++ 11标准§3.4.2 / 1指出: 当函数调用(5.2.2)中的postfix-expression是非限定id时,可能会搜索在通常的非限定查找(3

Namespaces and the Pre

I'm having some trouble understanding the preprocessor and namespaces in C++. For example, consider the following program: #include <iostream> int main() { using namespace std; cout << "Hello World!" << endl; return 0; } So when this program is getting ready to be compiled, the preprocessor will recognize the #include directive and add the iostream file

命名空间和Pre

我在理解C ++中的预处理器和命名空间时遇到了一些麻烦。 例如,请考虑以下程序: #include <iostream> int main() { using namespace std; cout << "Hello World!" << endl; return 0; } 所以当这个程序准备好编译时,预处理器会识别#include指令并将iostream文件添加到程序中,以便程序具有I / O功能(即“cout”和“endl”)。 现在根据我的教科书,作为C ++编译器标准组件的类,函数和变

Combining C++ and C

I'm working on a project that has a lot of legacy C code. We've started writing in C++, with the intent to eventually convert the legacy code, as well. I'm a little confused about how the C and C++ interact. I understand that by wrapping the C code with extern "C" the C++ compiler will not mangle the C code's names, but I'm not entirely sure how to implement this.

结合C ++和C

我正在研究一个有很多遗留C代码的项目。 我们已经开始用C ++编写,并且最终还是要转换遗留代码。 我对C和C ++如何交互有点困惑。 我明白,通过用extern "C"包装C代码,C ++编译器不会破坏C代码的名称,但我不完全确定如何实现这一点。 所以,在每个C头文件的顶部(在包含守卫之后),我们有 #ifdef __cplusplus extern "C" { #endif 在底部,我们写 #ifdef __cplusplus } #endif 在这两者之间,我们拥有所有

Is it a good practice to use long int to avoid overflow?

Suppose I have declared following variables : some_dataType a,b,c; //a,b,c are positive int res; res=a+b-c; And I have been provided with the information that the variable res cannot cross integer limit. Just to avoid overflow from the expression a+b in a+bc I have following options- declare a,b,c as long int (ac) +b (long int) (a+bc) //or some other type casting My question is which

使用long int避免溢出是一种很好的做法吗?

假设我已经声明了以下变量: some_dataType a,b,c; //a,b,c are positive int res; res=a+b-c; 我已经提供了变量res不能超过整数限制的信息。 只是为了避免溢出从表达式a+b在a+bc我具有以下选项- 将int,b,c声明为long int (ac) + b (long int) (a + bc)//或其他类型的投射 我的问题是以上哪一点是好的做法。 另外,为了逃避这一切,我更喜欢做选项1.但是它可能会增加程序的内存大小(不是为了这个程序,而是为

Cluster member variables declaration by their type useful or not?

Please have a look a the following code sample, executed on a Windows-32 system using Visual Studio 2010: #include <iostream> using namespace std; class LogicallyClustered { bool _fA; int _nA; char _cA; bool _fB; int _nB; char _cB; }; class TypeClustered { bool _fA; bool _fB; char _cA; char _cB; int _nA; int _nB; }; int main(int argc,

集群成员变量声明的类型是否有用?

请看下面的代码示例,它使用Visual Studio 2010在Windows-32系统上执行: #include <iostream> using namespace std; class LogicallyClustered { bool _fA; int _nA; char _cA; bool _fB; int _nB; char _cB; }; class TypeClustered { bool _fA; bool _fB; char _cA; char _cB; int _nA; int _nB; }; int main(int argc, char* argv[]) { cout << siz

Why is iostream::eof inside a loop condition considered wrong?

I just found a comment in this answer saying that using iostream::eof in a loop condition is "almost certainly wrong". I generally use something like while(cin>>n) - which I guess implicitly checks for EOF, why is checking for eof explicitly using iostream::eof wrong? How is it different from using scanf("...",...)!=EOF in C (which I often use with no problems)? Bec

为什么iostream :: eof在循环中被认为是错误的?

我刚刚在这个回答中发现了一条评论,说在循环条件下使用iostream::eof “几乎肯定是错误的”。 我通常使用像while(cin>>n) - 我猜隐式检查EOF,为什么显式检查使用iostream::eof错误的iostream::eof ? 它与C中使用scanf("...",...)!=EOF (我经常使用没有问题)有什么不同? 因为iostream::eof只会在读取流结束后才返回true 。 它并不表示,下一次读取将是流的结束。 考虑这一点(并假设接下来的阅读将在

Is there any overhead to declaring a variable within a loop? (C++)

This question already has an answer here: Difference between declaring variables before or in loop? 24 answers Stack space for local variables is usually allocated in function scope. So no stack pointer adjustment happens inside the loop, just assigning 4 to var . Therefore these two snippets have the same overhead. For primitive types and POD types, it makes no difference. The compiler

在循环中声明一个变量是否有任何开销? (C ++)

这个问题在这里已经有了答案: 在循环之前或循环中声明变量之间的区别? 24个答案 局部变量的堆栈空间通常在函数范围内分配。 所以在循环内部不会发生堆栈指针调整,只需将var指定为4即可。 因此这两个片段具有相同的开销。 对于原始类型和POD类型,它没有区别。 编译器将在函数的开头为变量分配堆栈空间,并在两种情况下函数返回时释放该空间。 对于具有非平凡构造函数的非POD类类型,它会有所作为 - 在这种情况下

How to implement classic sorting algorithms in modern C++?

The std::sort algorithm (and its cousins std::partial_sort and std::nth_element ) from the C++ Standard Library is in most implementations a complicated and hybrid amalgamation of more elementary sorting algorithms, such as selection sort, insertion sort, quick sort, merge sort, or heap sort. There are many questions here and on sister sites such as https://codereview.stackexchange.com/ related

如何在现代C ++中实现经典的排序算法?

来自C ++标准库的std::sort算法(和它的cousins std::partial_sort和std::nth_element )在大多数实现中是一个更复杂和混合的更多基本排序算法的混合,比如选择排序,插入排序,快速排序,合并排序或堆排序。 这里和姊妹网站上存在很多问题,例如https://codereview.stackexchange.com/与错误,复杂性以及这些经典排序算法实现的其他方面有关。 大多数提供的实现都由原始循环组成,使用索引操作和具体类型,并且通常在正确性

Why are C++ inline functions in the header?

NB This is not a question about how to use inline functions or how they work, more why they are done the way they are. The declaration of a class member function does not need to define a function as inline , it is only the actual implementation of the function. For example, in the header file: struct foo{ void bar(); // no need to define this as inline } So why does the inline implement

为什么头文件中的C ++内联函数?

注意这不是一个关于如何使用内联函数或它们如何工作的问题,更不是为什么它们按照它们的方式完成。 类成员函数的声明不需要将函数定义为inline函数,它只是函数的实际实现。 例如,在头文件中: struct foo{ void bar(); // no need to define this as inline } 那么为什么类函数的内联实现必须位于头文件中呢? 为什么我不能把内联函数放在.cpp文件中? 如果我在哪里尝试将内联定义放入.cpp文件中,我会收到以下错误