c++-faq

What to watch out for when converting a std::string to a char* for C function?

百般思念 提交于 2019-12-06 18:49:15
问题 I have read many posts asking the question on how to convert a C++ std::string or const std::string& to a char* to pass it to a C function and it seems there is quite a few caveat's in regards to doing this. One has to beware about the string being contiguous and a lot of other things. The point is that I've never really understood all the points one needs to be aware of and why ? I wondered if someone could sum up the caveats and downfalls about doing a conversion from a std::string to a

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

半城伤御伤魂 提交于 2019-12-06 16:06:32
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members? Kevin 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-degradation. In addition, atomicity and other concurrency-guarantees might be broken, leading to

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

和自甴很熟 提交于 2019-12-06 06:08:58
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? Greg Hewgill This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar: using namespace foo; using namespace bar; Everything works fine, and you can call Blah() from Foo and Quux() from Bar

Valid expressions for default function arguments

人走茶凉 提交于 2019-12-05 11:18:53
What are all the possible types of valid expressions for a default argument in a function or member function? lapk Anything that is correct within context of assignment to a variable of function parameter's type. Edit The default arguments during compilation are evaluated in terms of type correctness etc, but they are not calculated and no assignment takes place until run-time. You can specify a constructor of a yet to be defined class as a default argument and it's fine, as long as class is defined at the point of function use... The actual calculation/assignment takes place during function

C++17 lambda capture *this

旧时模样 提交于 2019-12-03 23:33:22
C++17 will add copy capture of this object by value, with a capture specification of [*this] . How is this useful? How is it different than capturing this ? Can't this already be achieved in C++14 with [tmp = *this] ? Bonus for explaining why P0018R3 uses [=, tmp = *this] instead of [tmp = *this] in their example. If they had used [tmp = *this] , all the listed downsides of the C++14 solution would be eliminated. How is it useful? It's useful when you need a copy of *this - for example, when *this itself is no longer valid by the time the lambda is evaluated. How is it different from capturing

Where can I find all the exception guarantees for the Standard Containers and Algorithms?

北慕城南 提交于 2019-12-03 04:17:07
问题 Yes, I've looked at the C++ standards that I could find (or the drafts), but I'm not finding any comprehensive of the exception guarantees given by STL containers. All I can find are occasional sections with incomplete descriptions on some of the functions for some of the types. Or perhaps it's there but I'm just not finding it, I don't know. Note: I'm not asking for a list of all the guarantees people can think of, which is basically in this question. I'm looking for the authoritative source

What is the difference between the standard library and the standard template library?

左心房为你撑大大i 提交于 2019-12-02 21:29:10
I keep seeing reference to both the C++ standard Library and the C++ Standard Template Library (STL). What is the difference between them? Wikipedia mentions that they share some headers but that's about it. The Standard Template Library (STL) is a library of containers, iterators, algorithms, and function objects, that was created by Alexander Stepanov; the SGI website has the canonical implementation and documentation. The standard library is the library that is part of C++; it includes most of the Standard Template Library (STL). In common usage, "STL" is also used to refer to the parts of

Where can I find all the exception guarantees for the Standard Containers and Algorithms?

泄露秘密 提交于 2019-12-02 17:35:46
Yes, I've looked at the C++ standards that I could find (or the drafts), but I'm not finding any comprehensive of the exception guarantees given by STL containers. All I can find are occasional sections with incomplete descriptions on some of the functions for some of the types. Or perhaps it's there but I'm just not finding it, I don't know. Note: I'm not asking for a list of all the guarantees people can think of, which is basically in this question . I'm looking for the authoritative source of this information itself -- or preferably, a free version of the source (e.g. a draft of the

What are the differences between struct and class in C++?

∥☆過路亽.° 提交于 2019-12-01 15:10:29
This question was already asked in the context of C#/.Net . Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design. I'll start with an obvious difference: If you don't specify public: or private: , members of a struct are public by default; members of a class are private by default. I'm sure there are other differences to be found in the obscure corners of the C++ specification. Assaf Lavie You forget the tricky 2nd difference between classes and structs. Quoth the

What's the difference between the terms “source file” and “translation unit”?

若如初见. 提交于 2019-11-28 22:44:30
What's the difference between source file and translation unit? There is nothing we can do From the C++ Standard: A source file together with all the headers and source files included via the preprocessing directive #include less any source line skipped by any of the conditional inclusion preprocessing directives is called a translation unit. A "translation unit" is a source file plus any headers or other source files it #includes, plus any files that THEY include, and so on. A source file is just that...one source file. If it helps any, think of the source file as the "before" the