c++-faq

Explain C++ SFINAE to a non-C++ programmer

爱⌒轻易说出口 提交于 2019-12-17 05:37:26
问题 What is SFINAE in C++? Can you please explain it in words understandable to a programmer who is not versed in C++? Also, what concept in a language like Python does SFINAE correspond to? 回答1: Warning: this is a really long explanation, but hopefully it really explains not only what SFINAE does, but gives some idea of when and why you might use it. Okay, to explain this we probably need to back up and explain templates a bit. As we all know, Python uses what's commonly referred to as duck

Should I include <xxxx.h> or <cxxxx> in C++ programs?

南楼画角 提交于 2019-12-17 04:17:11
问题 What should I include in C++ programs, stdio.h or cstdio ? and Why? Why two header files which provide the same functionality? What does the standard say regarding this? How should I go about including other such headers, Is there a base rule that I should follow? 回答1: Consider the following programs: Sample 1: #include<stdio.h> int main() { printf("Hello World"); return 0; } Sample 2: #include<cstdio> int main() { printf("Hello World"); return 0; } Both work as expected. So which usage is

Should I include <xxxx.h> or <cxxxx> in C++ programs?

℡╲_俬逩灬. 提交于 2019-12-17 04:17:03
问题 What should I include in C++ programs, stdio.h or cstdio ? and Why? Why two header files which provide the same functionality? What does the standard say regarding this? How should I go about including other such headers, Is there a base rule that I should follow? 回答1: Consider the following programs: Sample 1: #include<stdio.h> int main() { printf("Hello World"); return 0; } Sample 2: #include<cstdio> int main() { printf("Hello World"); return 0; } Both work as expected. So which usage is

What are template deduction guides and when should we use them?

不打扰是莪最后的温柔 提交于 2019-12-17 03:30:54
问题 The C++17 standard introduces "template deduction guides". I gather they're something to do with the new template argument deduction for constructors introduced in this version of the standard, but I haven't yet seen a simple, FAQ-style explanation of what they are and what they're for. What are template deduction guides in C++17? Why (and when) do we need them? How do I declare them? 回答1: Template deduction guides are patterns associated with a template class that tell the compiler how to

Why is 'this' a pointer and not a reference?

我与影子孤独终老i 提交于 2019-12-17 02:30:12
问题 I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56 That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35 I'd

Why are C++ inline functions in the header?

瘦欲@ 提交于 2019-12-17 02:18:10
问题 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 implementation of a classes function have to be in the header file? Why can't I put the inline function the .cpp

What does it mean to have an undefined reference to a static member?

独自空忆成欢 提交于 2019-12-16 22:51:19
问题 I just wrote a class with some static data members, but now I am getting errors about "undefined references". Why doesn't this work? What am I doing wrong? (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 started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is

Which iomanip manipulators are 'sticky'?

一笑奈何 提交于 2019-12-16 20:12:15
问题 I recently had a problem creating a stringstream due to the fact that I incorrectly assumed std::setw() would affect the stringstream for every insertion, until I changed it explicitly. However, it is always unset after the insertion. // With timestruct with value of 'Oct 7 9:04 AM' std::stringstream ss; ss.fill('0'); ss.setf(ios::right, ios::adjustfield); ss << setw(2) << timestruct.tm_mday; ss << timestruct.tm_hour; ss << timestruct.tm_min; std::string filingTime = ss.str(); // BAD: '0794'

What are transparent comparators?

你说的曾经没有我的故事 提交于 2019-12-16 20:00:49
问题 In C++14, associative containers seem to have changed from C++11 – [associative.reqmts]/13 says: The member function templates find , count , lower_bound , upper_bound , and equal_range shall not participate in overload resolution unless the type Compare::is_transparent exists. What is the purpose of making an comparator "transparent"? C++14 also provides library templates like this: template <class T = void> struct less { constexpr bool operator()(const T& x, const T& y) const; typedef T

Why should I always enable compiler warnings?

不打扰是莪最后的温柔 提交于 2019-12-16 20:00:47
问题 I often hear that when compiling C and C++ programs I should "always enable compiler warnings". Why is this necessary? How do I do that? Sometimes I also hear that I should "treat warnings as errors". Should I? How do I do that? 回答1: Why enable warnings? C and C++ compilers are notoriously bad at reporting some common programmer mistakes by default , such as: forgetting to initialise a variable forgetting to return a value from a function arguments in printf and scanf families not matching