c++-faq

Why use apparently meaningless do-while and if-else statements in macros?

北慕城南 提交于 2019-11-25 22:14:22
问题 In many C/C++ macros I\'m seeing the code of the macro wrapped in what seems like a meaningless do while loop. Here are examples. #define FOO(X) do { f(X); g(X); } while (0) #define FOO(X) if (1) { f(X); g(X); } else I can\'t see what the do while is doing. Why not just write this without it? #define FOO(X) f(X); g(X) 回答1: The do ... while and if ... else are there to make it so that a semicolon after your macro always means the same thing. Let's say you had something like your second macro.

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

百般思念 提交于 2019-11-25 22:12:49
问题 What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast (type)value Function-style cast type(value) How does one decide which to use in which specific cases? 回答1: static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float , or pointer to void* ), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating static_cast isn't necessary,

How do I use arrays in C++?

被刻印的时光 ゝ 提交于 2019-11-25 22:11:10
问题 C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone ( std::vector<T> since C++98 and std::array<T, n> since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read legacy code or interact with a library written in C, you should have a firm grasp on how arrays work. This FAQ is split into five parts: arrays on the type level and accessing elements array creation

What is the copy-and-swap idiom?

我怕爱的太早我们不能终老 提交于 2019-11-25 22:09:51
问题 What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it\'s been mentioned in many places, we didn\'t have any singular \"what is it\" question and answer, so here it is. Here is a partial list of places where it was previously mentioned: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor and = operator overload in C++: is a common function possible? What is copy elision and how it optimizes copy

What are the basic rules and idioms for operator overloading?

被刻印的时光 ゝ 提交于 2019-11-25 22:09:38
问题 Note: The answers were given in a specific order , but since many users sort answers according to votes, rather than the time they were given, here\'s an index of the answers in the order in which they make most sense: The General Syntax of operator overloading in C++ The Three Basic Rules of Operator Overloading in C++ The Decision between Member and Non-member Common operators to overload Assignment Operator Input and Output Operators Function call operator Comparison operators Arithmetic

Why is “using namespace std;” considered bad practice?

99封情书 提交于 2019-11-25 22:09:18
问题 I\'ve been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead. Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance? 回答1: This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar: using namespace foo;

What is an undefined reference/unresolved external symbol error and how do I fix it?

此生再无相见时 提交于 2019-11-25 22:09:14
问题 What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them? Feel free to edit/add your own. 回答1: Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference): The precedence among the syntax rules of translation is specified by the following phases [see footnote] . Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set

Why doesn&#39;t a simple “Hello World”-style program compile with Turbo C++?

我怕爱的太早我们不能终老 提交于 2019-11-25 22:07:59
I have started learning C++ for my programming class. I have downloaded this "Hello World" program: #include <iostream> using namespace std; int main() { cout << "Hello, World!"; return 0; } but Turbo C++ complains: Error D:\HELLO.CPP 1: Unable to open include file 'IOSTREAM' Error D:\HELLO.CPP 2: Declaration syntax error Error D:\HELLO.CPP 6: Undefined symbol 'cout' What's wrong with this very simple program? How can I correct these errors? n.m. There's no problem with this program. (Except probably some stylistic issues — using namespace std is not recommended). The problem is with Turbo C++

What is external linkage and internal linkage?

眉间皱痕 提交于 2019-11-25 21:57:46
问题 I want to understand the external linkage and internal linkage and their difference. I also want to know the meaning of const variables internally link by default unless otherwise declared as extern . 回答1: When you write an implementation file ( .cpp , .cxx , etc) your compiler generates a translation unit . This is the object file from your implementation file plus all the headers you #include d in it. Internal linkage refers to everything only in scope of a translation unit . External

What is the proper declaration of main?

戏子无情 提交于 2019-11-25 21:57:32
问题 What is the proper signature of the main function in C++? What is the correct return type, and what does it mean to return a value from main ? What are the allowed parameter types, and what are their meanings? Is this system-specific? Have those rules changed over time? What happens if I violate them? 回答1: The main function must be declared as a non-member function in the global namespace. This means that it cannot be a static or non-static member function of a class, nor can it be placed in