c++-standard-library

How is std::is_constructible<T, Args> implemented? [duplicate]

淺唱寂寞╮ 提交于 2019-12-19 17:47:27
问题 This question already has answers here : C++98/03 std::is_constructible implementation (2 answers) Closed 2 years ago . So far I can't find anything ELI5 online. For a learning project I would like to implement my own is_constructible. Can someone explain how it works, please? 回答1: From cppreference: [I]f the variable definition T obj(std::declval<Args>()...); is well-formed, value is equal to true , else value is false . Checking whether code is well-formed can be done with SFINAE techniques

Copy std::map into std::vector of pairs

五迷三道 提交于 2019-12-19 12:28:09
问题 I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr){ vectorWordsList.push_back(*itr); } sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b){return a.second > b.second;}); } I need to find a way to do this without using a raw loop, using

Does the C++ standard library have a set ordered by insertion order?

落花浮王杯 提交于 2019-12-19 05:21:57
问题 Does the C++ standard library have an "ordered set" datastructure? By ordered set, I mean something that is exactly the same as the ordinary std::set but that remembers the order in which you added the items to it. If not, what is the best way to simulate one? I know you could do something like have a set of pairs with each pair storing the number it was added in and the actual value, but I dont want to jump through hoops if there is a simpler solution. 回答1: No single, homogeneous data

What's the deal with setw()?

随声附和 提交于 2019-12-19 05:11:30
问题 I recently was bitten by the fact that ios_base::width and/or the setw manipulator have to be reset with every item written to the stream. That is, you must do this: while(whatever) { mystream << std::setw(2) << myval; } Rather than this: mystream.width(2); while(whatever) { mystream << myval; } Ok, fine. But does anyone know why this design decision was made? Is there some rationale that I'm missing, or is this just a dark corner of the standard? Other stream formatting modifiers (as

Why will std::rel_ops::operators be deprecated in C++20?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 05:06:38
问题 According to cppreference.com, std::rel_ops::operator!=,>,<=,>= will be deprecated in C++20. What's the rationale behind? 回答1: In C++20, you get three-way comparison (operator <=> ), which automatically "generates" default comparisons if provided: struct A { // You only need to implement a single operator. std::strong_ordering operator<=>(const A&) const; }; // Compiler generates all 6 relational operators A to1, to2; if (to1 == to2) { /* ... */ } // ok if (to1 <= to2) { /* ... */ } // ok,

std::isinf does not work with -ffast-math. how to check for infinity

雨燕双飞 提交于 2019-12-18 16:56:30
问题 Sample code: #include <iostream> #include <cmath> #include <stdint.h> using namespace std; static bool my_isnan(double val) { union { double f; uint64_t x; } u = { val }; return (u.x << 1) > 0x7ff0000000000000u; } int main() { cout << std::isinf(std::log(0.0)) << endl; cout << std::isnan(std::sqrt(-1.0)) << endl; cout << my_isnan(std::sqrt(-1.0)) << endl; cout << __isnan(std::sqrt(-1.0)) << endl; return 0; } Online compiler. With -ffast-math , that code prints "0, 0, 1, 1" -- without, it

Are C++ standard library implementations allowed to strengthen noexcept specifications?

本秂侑毒 提交于 2019-12-18 12:54:29
问题 According to the C++ standard, are implementations of the C++ standard library allowed to strengthen noexcept specifications of methods and other functions of the C++ standard library as defined by the standard? For example, if the C++ standard specifies some function std::f as void f(); are standard library implementations allowed to implement it as void f() noexcept; instead? 回答1: The Standard says yes: § 17.6.5.12.1 Restrictions on exception handling [res.on.exception.handling] Any of the

How to inspect std::string in GDB with no source code?

天大地大妈咪最大 提交于 2019-12-18 10:47:07
问题 I'm trying to debug a program that has no source code available, and I need to look at what it has stored in a std::string. I've been Googling and looking on here, and I've found some information about outputting STL containers, but all of it refers to variables, with no source or debug information all I have is a memory offset of the class data. Is there any way to do this? 回答1: Every std::string implementation has a pointer to the raw characters in it somewhere. For g++ 4.x , that pointer

Implementing a “string pool” that is guaranteed not to move

余生颓废 提交于 2019-12-18 04:48:16
问题 I need a "string pool" object into which I can repeatedly insert a "sequence of chars" (I use this phrase to mean "string" without confusing it with std::string or a C string), obtain a pointer to the sequence, and be guaranteed that the pointer will not become invalidated if/when the pool needs to grow. Using a simple std::string as the pool won't work, because of the possibility for the string to be reallocated when it outgrows its initial capacity, thus invalidating all previous pointers

Android std and stl support

心已入冬 提交于 2019-12-17 22:24:36
问题 I am playing with android ndk. I am using Window Vista with cygwin (latest version). I compiled and launched the hello world jni sample on my phone. It is working. The code is (is a .cpp file): #include <string.h> #include <jni.h> extern "C" { JNIEXPORT jstring JNICALL Java_org_android_helloworld_HelloworldActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis); }; jstring Java_org_android_helloworld_HelloworldActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) { return env-