c++-standard-library

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

浪尽此生 提交于 2019-11-30 14:43:13
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 prints "1, 1, 1, 1". Is that correct? I thought that std::isinf / std::isnan should still work with -ffast

Is there any std::chrono thread safety guaranty even with multicore context?

依然范特西╮ 提交于 2019-11-30 13:47:30
问题 First, I'm assuming that calling any function of std::chrono is guaranteed to be thread-safe (no undefined behaviour or race conditions or anything dangerous if called from different threads). Am I correct? Next, for example on windows there is a well known problem related to multi-core processors that force some implementations of time related systems to allow forcing a specific core to get any time information. What I want to know is: using std::chrono, in the standard, is there any

Standard library facilities which allocate but don't use an Allocator

狂风中的少年 提交于 2019-11-30 12:56:47
In most places where the C++ standard library allocates memory, the user is able to customise this by providing a class which meets the Allocator requirements . For example, almost all containers take an allocator template argument, and std::allocate_shared returns a shared_ptr whose contained element and control block are both allocated via a provided Allocator. However, there are a few places where the standard library can (potentially) allocate memory, but no Allocator support is provided. The ones I can think of are: std::make_unique() (no corresponding allocate_unique() ) std::any std:

Is there any std::chrono thread safety guaranty even with multicore context?

对着背影说爱祢 提交于 2019-11-30 08:35:04
First, I'm assuming that calling any function of std::chrono is guaranteed to be thread-safe (no undefined behaviour or race conditions or anything dangerous if called from different threads). Am I correct? Next, for example on windows there is a well known problem related to multi-core processors that force some implementations of time related systems to allow forcing a specific core to get any time information . What I want to know is: using std::chrono, in the standard, is there any guarantee that think kind of problem shouldn't appear? or is it implementation defined or is there an

What's the difference between input iterators and read-only forward iterators?

北城以北 提交于 2019-11-30 08:06:27
问题 What's the difference between input iterators and read-only forward iterators? Because the latter are read-only, they obviously don't satisfy requirements of output iterators. And, because of that, they're effectively input iterators with additional guarantees (if any). The problem is, what additional guarantees? My guess would be that forward iterators are multi-pass and input iterators are not, am I right? 回答1: Yes, input iterators are one-pass iterators. You can only iterate over them once

Is using std::vector< std::shared_ptr<const T> > an antipattern?

那年仲夏 提交于 2019-11-30 06:03:01
For a long time I was using std::vector and std::shared_ptr hand in hand. Recently I started using std::shared_ptr<const T> whenever a pointer to a const object was needed. This is all OK, since std::shared_ptr<T> can be cast to std::shared_ptr<const T> and then they share the same reference counter and everything feels natural. But when I try to use constructs such as std::vector< std::shared_ptr<const T> > I run into troubles. To simplify I will denote the two structures: template <class T> using SharedPtrVector = std::vector< std::shared_ptr<T> >; template <class T> using

Character Array as a value in C++ map

こ雲淡風輕ζ 提交于 2019-11-30 05:03:05
问题 I want to define something like Map<int, char[5] > myMap; The above declaration is accepted by c++ compiler and no error is thrown but when I do something like this int main() { char arr[5] ="sdf"; map <int, char[5]> myMap; myMap.insert(pair<int, char[5]>(0,arr)); return 0; } I get error as: In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0, from /usr/include/c++/4.6/bits/char_traits.h:41, from /usr/include/c++/4.6/ios:41, from /usr/include/c++/4.6/ostream:40, from /usr

Are there any STL headers which are not part of the C++ Standard Library?

我只是一个虾纸丫 提交于 2019-11-30 04:47:40
I know that some C++ Standard Library headers are originated from the STL, such as vector . But I'm failing to find an up-do-date list of STL headers which are still not incorporated by the Standard Library. Do they exist? PS: I would like to have them listed, and also to know if all major implementations include them or where to get them, if possible. Note, this is a function by function break down, rather than a by header breakdown, because it seems to be more useful. If we examine SGI's documentation of the STL we find the following: slist has been renamed std::forward_list . bit_vector has

STL-pair-like triplet class - do I roll my own?

三世轮回 提交于 2019-11-30 00:15:00
问题 I want to use a triplet class, as similar as possible to std::pair. STL doesn't seem to have one. I don't want to use something too heavy, like Boost. Is there some useful FOSS non-restrictive-license triplet class I could lift from somewhere? Should I roll my own? Should I do something else entirely? Edit: About std::tuple ... Is there really no benefit to a triplet-specific class? I mean, with tuple, I can't do template<typename T1, typename T2, typename T3> std::tuple<T1, T2, T3> triple;

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

空扰寡人 提交于 2019-11-29 23:10: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? Every std::string implementation has a pointer to the raw characters in it somewhere. For g++ 4.x , that pointer is at offset 0 into the string. If you know that the string resides at e.g. 0x7fffffffda88 , then print *