c++-faq

Where and why do I have to put the “template” and “typename” keywords?

空扰寡人 提交于 2019-12-13 03:24:54
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

Undefined behavior and sequence points

拈花ヽ惹草 提交于 2019-12-12 17:09:39
问题 What are "sequence points"? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i; , to make myself feel better. Why should I stop using them? If you've read this, be sure to visit the follow-up question Undefined behavior and sequence points reloaded . (Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that

What are the basic rules and idioms for operator overloading?

牧云@^-^@ 提交于 2019-12-12 05:14:08
问题 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

What are copy elision and return value optimization?

倖福魔咒の 提交于 2019-12-12 05:02:21
问题 What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you're probably looking for the introduction. For a technical overview, see the standard reference. See common cases here. 回答1: Introduction For a technical overview - skip to this answer. For common cases where copy elision occurs - skip to this answer. Copy elision is an optimization implemented by most

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

萝らか妹 提交于 2019-12-12 02:44:16
问题 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

What are the basic rules and idioms for operator overloading?

亡梦爱人 提交于 2019-12-12 02:39:14
问题 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

Where and why do I have to put the “template” and “typename” keywords?

江枫思渺然 提交于 2019-12-11 17:13:16
问题 In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code: template <typename T, typename Tail> // Tail will be a UnionNode too. struct UnionNode : public Tail { // ... template<typename U> struct inUnion { // Q: where to add typename/template here? typedef Tail::inUnion<U> dummy; }; template< > struct inUnion<T> { }; }; template <typename T> // For the last node Tn. struct UnionNode<T, void> { //

What are copy elision and return value optimization?

寵の児 提交于 2019-12-11 12:24:26
问题 What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you're probably looking for the introduction. For a technical overview, see the standard reference. See common cases here. 回答1: Introduction For a technical overview - skip to this answer. For common cases where copy elision occurs - skip to this answer. Copy elision is an optimization implemented by most

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

可紊 提交于 2019-12-10 10:40:17
问题 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; using

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

我的未来我决定 提交于 2019-12-08 03:30:04
问题 Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members? 回答1: This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs: Mis-aligned access might be a hard error (often SIGBUS ). Mis-aligned access might be a soft error. Either corrected in hardware, for a modest performance-degradation. Or corrected by emulation in software, for a severe performance