c++-standard-library

Gnu C++ macro __cplusplus standard conform?

自作多情 提交于 2019-12-03 09:24:15
The Gnu C++ compiler seems to define __cplusplus to be 1 #include <iostream> int main() { std::cout << __cplusplus << std::endl; } This prints 1 with gcc in standard c++ mode, as well as in C++0x mode, with gcc 4.3.4 , and gcc 4.7.0. The C++11 FDIS says in "16.8 Predefined macro names [cpp.predefined]" that The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit. (Footnote: It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming com- pilers should use a value with at most five decimal

Is Stephen Lavavej's Mallocator the same in C++11?

风格不统一 提交于 2019-12-03 06:38:43
8 years ago, Stephen Lavavej published this blog post containing a simple allocator implementation, named the "Mallocator". Since then we've transitioned to the era of C++11 (and soon C++17) ... does the new language features and rules affect the Mallocator at all, or is it still relevant as is? STL himself has an answer to this question in his STL Features and Implementation techniques talk at CppCon 2014 (Starting at 26'30). The slides are on github. I merged the content of slides 28 and 29 below: #include <stdlib.h> // size_t, malloc, free #include <new> // bad_alloc, bad_array_new_length

How to get the file size in bytes with C++17

坚强是说给别人听的谎言 提交于 2019-12-03 06:28:11
问题 Are there pitfalls for specific operating systems, I should know of? There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today. Methods from other (old QA's) on .sx stat.h (wrapper sprintstatf), uses syscall tellg(), returns per definition a position but not necessarily bytes . The return type is not int . 回答1: <filesystem> (added in C++17) makes this very straightforward. #include

Why is `std::byte` an enum class instead of a class?

怎甘沉沦 提交于 2019-12-03 05:53:58
std::byte is an abstraction that is supposed to provide a type safe(r) access to regions of memory in C++, starting with the new standard 17. However, it's declared this way according to http://en.cppreference.com/w/cpp/types/byte : enum class byte : unsigned char {} ; That is, it is an enum class without any enumerations. Since usually the purpose of enums is to provide a restricted set of enumerations, this seems a bit strange. A class with a private unsigned char member seems like the more obvious way to do this. Why is it done this way? A class with an unsigned char member would not be

Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

无人久伴 提交于 2019-12-03 04:19:17
For example, #include <iostream> int main() { unsigned n{}; std::cin >> n; std::cout << n << ' ' << (bool)std::cin << std::endl; } When input -1 , clang 6.0.0 outputs 0 0 while gcc 7.2.0 outputs 4294967295 1 . I'm wondering who is correct. Or maybe both are correct for the standard does not specify this? By fail, I take to mean (bool)std::cin be evaluated false. clang 6.0.0 fails input -0 too. As of Clang 9.0.0 and GCC 9.2.0, both compilers, using either libstdc++ or libc++ in the case of Clang, agree on the result of the program above, independent of the C++ version (>= C++11) used, and print

Should an STL container avoid copying elements into themselves when the container is copied into itself?

冷暖自知 提交于 2019-12-03 01:21:43
The question is about self-assignment. For example copying a vector into itself: std::vector<std::string> vec(5, "hello"); vec = vec; Should the code above perform 5 assignment operations of strings into themselves, or just do nothing? I mean whether the following checking is valid: std::vector operator=(const std::vector &rhs) { if (this == &rhs) { return *this; } ... } I'm working on my own implementation of std::variant class (just for fun) and interested if I should add a self-assignment check to the beginning of the assignment operator, or should I just copy the contained element into

How to get the file size in bytes with C++17

时光怂恿深爱的人放手 提交于 2019-12-02 19:52:47
Are there pitfalls for specific operating systems, I should know of? There are many duplicates ( 1 , 2 , 3 , 4 , 5 ) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today. Methods from other (old QA's) on .sx stat.h (wrapper sprintstatf ), uses syscall tellg() , returns per definition a position but not necessarily bytes . The return type is not int . <filesystem> (added in C++17) makes this very straightforward . #include <cstdint> #include <filesystem> // ... std::uintmax_t size = std::filesystem::file_size("c:\\foo\\bar

iterator returned by std::find() is not dereferenceable

陌路散爱 提交于 2019-12-02 16:06:06
问题 This is an insert() function of an implementation of a HashTable with chaining. In order to avoid duplications in the linked_list I ckecked if a value already exists. If it does then I just replace the existing value as it can be seen almost at the end where it comments "update value". That line issues an exception telling me that the iterator is not dereferenceable. Why can't I dereference the iterator returned by std::find()? Is there another way to update the value that was found? virtual

iterator returned by std::find() is not dereferenceable

梦想的初衷 提交于 2019-12-02 10:20:51
This is an insert() function of an implementation of a HashTable with chaining. In order to avoid duplications in the linked_list I ckecked if a value already exists. If it does then I just replace the existing value as it can be seen almost at the end where it comments "update value". That line issues an exception telling me that the iterator is not dereferenceable. Why can't I dereference the iterator returned by std::find()? Is there another way to update the value that was found? virtual void insert(const K& k, const V& v) { auto index = hashFctn(k, m_table.capacity()); if (needsToGrow() |

C++: string operator overload

浪子不回头ぞ 提交于 2019-12-02 02:41:33
Can I overload existing function/operator in existing class? I was trying to do: #include <iostream> #include <string> using namespace std; string& string::operator<<(const string& str) { this->append(str); } But this gives me error: test.cpp:5: error: too few template-parameter-lists How can I do this? Or I can't? You can't add member functions to a class unless you modify that class' definition. Use a free function instead: string& operator<<(string & lhs, const string & rhs) { return lhs += rhs; } I defer to Benjamin's answer for creating a stream-like interface on a string object. However,