Questions tagged [std]
The C++ Standard Library, and its namespace. Use in conjunction with [c++].
5,010
questions
-2
votes
0
answers
39
views
wrong ordering in std::priority_queue with c++20 3-way comparison operator (Space Ship Operator) [closed]
I was trying to implement a priority queue for a custom struct by defining the 3-way comparison operator for the struct. But looks like the ordering in the priority queue is incorrect. However, it ...
-2
votes
0
answers
80
views
How to break std::rand(). Predict rand() after many iterations [closed]
Everyone says not to use std::rand, but I don't understand why.
I have the following task: to find out the value of std::rand() after 10000000 iterations of next with different values passed to srand()...
1
vote
0
answers
61
views
Implementation of a custom zip iterator that works with std::ranges::transform as well as std:::transform [duplicate]
I have implemented a zip iterator, and it works well when used with algorithm functions like std::for_each or std::transform. However, when I use it with std::ranges::for_each or std::ranges::...
1
vote
0
answers
78
views
Can't build python package because C++ compiler error [duplicate]
I have tried to install python package that have C++ dependency under Windows 11 (in Google Colab / Kaggle notebook package builded just fine)
full log:
C:\Users\Dima>py -m pip install git+https://...
1
vote
0
answers
117
views
Is it safe to overload std::floor in C++17? [duplicate]
Important: question is about C++17, before std::floor became a template.
Lets say there is a user-defined template:
template<class Unit>
class Pixels {};
Is it safe to overload std::floor for ...
1
vote
2
answers
86
views
search for uses of int used instead of time_t
I have a rather large c-code base, where every once in a while int is used instead of time_t. Any good ideas as to how to deal with this. Should I search the entire standard library for functions that ...
6
votes
3
answers
158
views
Sum two std::array of non-default-constructible types
If I want to write a function summing two std::array, I would do something like:
template<class T, std::size_t N>
auto sum(const std::array<T, N>& a, const std::array<T, N>& ...
-3
votes
0
answers
18
views
I was writing a code from a teacher when i got hit with this error : was not declared in this scope [closed]
Im really new to C++ and as i was watching a teacher on youtube teaching me how to write C++ I was hit with this error when i copied the same exact code he wrote and he didnt get the error:
cool.cpp: ...
2
votes
1
answer
124
views
How is std::vector able to be constexpr without constexpr std::unitialized_copy?
I am implementing my own container and I was trying to make it as much constexpr friendly as possible.
In the process I found out that std::uninitialized_copy is not constexpr and therefore I can't ...
4
votes
1
answer
95
views
Create std::weak_ptr to an indexed value of an array-type std::shared_ptr?
I have a large dataset which essentially loads like this:
for(int i=0;i<count; i++){
auto ptr = std::make_shared<Object>(Object());
//do stuff with ptr
}
This isn't great for ...
0
votes
1
answer
56
views
The C++ standard defines _Cpp17Destructible_ is there a trait I can use to check this?
The C++ Standard defines Cpp17Destructible at https://eel.is/c++draft/utility.arg.requirements#:Cpp17Destructible:~:text=%5Btab%3Acpp17.destructible%5D
Is there a trait I can use to check this in code?...
0
votes
1
answer
115
views
How to use system() with std::thread together in C++
I am writing a C++ program on a win10 platform. I need to launch an external exe file in the second thread. The path of this external file is stored in a std::string object.
This external program will ...
-1
votes
1
answer
70
views
Using #include <cmath>, why it always select the long version of abs instead of choose due to the input type? [duplicate]
I've noticed this strange thing on an app I'm using (trying to recreate here, not sure if I'm able).
Basically, if I include #include <cmath> and I call abs() function (without namespace), ...
0
votes
1
answer
225
views
Is there a way to address more than one pair of elements in comparison predicate? [closed]
In the code below the values vector contains data indexed by indexes.
Background
It works just fine for sorting or searching while I have to deal with one value (actually, pair of them, lhs and rhs) ...
2
votes
1
answer
130
views
find() does not recognize my custom iterator
I wrote a custom iterator named Text_Iterator for a simple text editor. However, STL algorithms such as find() would not recognize it as a legitimate iterator.
My code is:
using Line = std::vector<...
0
votes
1
answer
49
views
error LNK2019 : unresolved external symbol __std_max_element_1 when using llvm from vcpkg
I just installed llvm 17.0.2#1 using vcpkg, the build went fine but now when I try to use it I get this linker error :
LLVMCore.lib(DataLayout.cpp.obj) : error LNK2019: unresolved external symbol ...
2
votes
2
answers
106
views
Is this behavior of std::expected with move-only types a MSVC bug or undefined behavior?
I've encountered a curious issue with my C++ code when compiling with Microsoft Visual Studio's MSVC compiler, and I'm trying to determine whether it's a compiler bug or undefined behavior according ...
2
votes
1
answer
106
views
Is there an algorithm in STL like std::unique which stores the amount of equal objects?
The std::unique algorithm keeps only unique elements in consecutive groups of elements. Meanwhile, sometimes it is useful to know how many specific elements were in the initial container. Like to make ...
-1
votes
1
answer
63
views
Why does `Allocator.allocate` hand out `NonNull<[u8]>`... but `deallocate` accepts `NonNull<u8>`? [duplicate]
As the title says.
pub unsafe trait Allocator {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(&self, ptr: NonNull<u8&...
0
votes
1
answer
108
views
Trying to upgrade / compile old C++ source (from a vendor) - problem with "std::runtime_error"
I'm trying to compile some sample code from a product we use. I don't think the vendor has maintained the code. I'm using VS Pro 2022 Ver 17.7.5
Here's the snipped of code and the errors it ...
1
vote
1
answer
91
views
Can't get std::array's data() method to work as a constexpr, am I doing something silly?
So, according to CPP reference, I should be able to use data() as a constexpr. My minimal example is as such:
#include <array>
#include <iostream>
int main(int argc,char** argv){
...
0
votes
1
answer
126
views
Why can't a nonstatic member function be referred to using the dot operator without calling it?
Consider the following
#include <iostream>
#include <functional>
class Foo {
private:
int m_num;
char m_letter;
public:
explicit Foo(int a, char c)
: m_num(a), ...
0
votes
1
answer
99
views
C++/STD - error: 'path' is unavailable: introduced in macOS 10.15
I am building QCefView on my MacBook M2 Max, MacOS 13.5 Ventura.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(CEFTest VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOUIC ON)
set(...
2
votes
1
answer
57
views
boost::has_range_const_iterator replacement with std
I have a big serialization library that rely on boost::has_range_const_iterator. The below code is part of a serialization library used in a game network.
template<typename T>
auto operator()(T ...
0
votes
3
answers
98
views
bad_weak_ptr when using enable_shared_from_this
I have a base class
class eventreceiver {
public:
eventreceiver() = default;
virtual ~eventreceiver() = default;
virtual void on_quit();
}
And I have a class which uses it
class engine : ...
0
votes
1
answer
135
views
Assign a function return-value to std::vector element: Different behavior in C++14 than in C++17
Assertion failed and has undefined behavior
#include<bits/stdc++.h>
const int MaxN = 1e5 + 10;
struct Node{
long long sum;
int left,right;
Node() :sum(0),left(0),right(0){
}
...
1
vote
1
answer
235
views
How an element of std::map can delete itself from the map?
I have two classes in different source file, class A in a.hpp and a.cpp, class B in b.hpp and b.cpp, A is a Singleton class, it create B object, and store it in a map, when B object iscreated, it ...
3
votes
1
answer
117
views
Any way for a function to return an execution policy?
I have recently been working on a project and I have encountered a problem where I didn't wanna use the std::execution::par on small vectors as that would create overhead.
Well theoretically that ...
3
votes
1
answer
128
views
Is this a correct convenience wrapper for std::expected?
If I am writing a function that returns an std::expected object and potentially calls other functions that return std::expected objects, I find myself writing code snippets like this very common.
...
5
votes
1
answer
163
views
How to convert `const std::vector<T*>` into `std::span<const T*>`
Title.
I am implementing this class:
#include <span>
#include <vector>
class MyClass {
public:
std::span<int *> numbers(void) {
return m_numbers;
}
std::span<...
0
votes
0
answers
123
views
Is it ok to use mutable to decrease all the keys in std::map by the same value?
As far as I know, std::map is almost always implemented as some kind of self-balancing search tree. Therefore, I assume decreasing every key by the same value should not result in undefined behaviors ...
2
votes
1
answer
159
views
C++ hash_map find() vs contains() performance
What's the better option performance-wise if I want to check if an element is contained in a map and use it directly afterwards?
std::unordered_map<int, std::string> my_map;
int my_key;
Option ...
2
votes
1
answer
121
views
Include Standard Libraries with cl.exe (in vanilla Command Prompt)?
I'm attempting to compile a simple shared library DLL file with a standard Windows Command Prompt command.
The DLL file will get compiled from the following C++ file called test.cpp:
#include <...
3
votes
1
answer
109
views
insert vs emplace when dealing with rvalues
std::string myString;
std::unordered_set<std::string> mySet;
mySet.insert(std::move(myString));
mySet.emplace(std::move(myString)); // this is an example, in reality you can't move twice
I ...
1
vote
1
answer
72
views
In std::unordered_map, how to iterate over hashes?
The std::unordered_map has hash-value for each key. What is the way to obtain those hash values?
What for? To evaluate relevance of a hash function to the data set. I could just generate hashes from ...
17
votes
1
answer
673
views
Why do I need to specify the type of a default constructed object in this situation?
I don't understand why in foobar below I need to specify std::vector<int>{} whereas in foobar2 I do not:
#include <iostream>
#include <memory>
#include <vector>
#include <...
0
votes
0
answers
43
views
How can I convert string with local encoding to UTF-16 wide string in C++?
I have a std::string with a known encoding, e.g. GB-18030. Is there a way to pass the string with the name of encoding to some functionality which converts the string to a UTF-16 wide string?
I don't ...
3
votes
0
answers
203
views
Why do std::ranges::binary_search and std::binary_search produce different results for the same input?
I don't understand why std::ranges::binary_search works different than std::binary_search. For example, this kind of code:
std::vector<int> A = { 0, 10, 12, 30 };
std::cout << std::ranges::...
0
votes
0
answers
33
views
Why .insert operator in map will not replace key value pair if same key exist? [duplicate]
//map key values are:
map[first] = 1, map[second]=5
map.insert(std::make_pair(second,15)); //still, map[second] =5
I know that map[second] = 15, will solve it but I wanted to know exactly why .insert ...
0
votes
2
answers
85
views
How to modernize a parallel C-style matrix-vector multiplication implementation using e.g. `std::transform`?
I am trying to adapt my programming practices with more modern approaches that leverage STL containers, algorithms, execution-policies, etc.
I developed a small test to experiment with modernizing my ...
4
votes
1
answer
117
views
Is it possible to swap the result of two std::tie calls
I have been trying for a while to find a standard compliant way to get something like this to compile:
int a = 1;
int b = 2;
int c = 3;
int d = 4;
std::swap(std::tie(a, b), std::tie(c, d));
(The use ...
2
votes
1
answer
65
views
How do I fix my `Any` class code to output data correctly?
I am trying to simulate std::any, my idea is to use the base class pointer to point to different types of template derived classes, to implement the function of storing different types of data, like ...
-2
votes
2
answers
130
views
Why std::vector doesn't use memcpy or realloc when resizing
I learnt that std::vector would explicitly call each element's copy-constructor during resizing, which if I understand correctly implies that it doesn't use memcpy or realloc under the hood, which was ...
-3
votes
1
answer
31
views
How can I have a gmock object return a fixed std::forward_list
I'm trying to write this test:
TEST(AccountServiceShould, print_a_statement_containing_all_transactions) {
auto transactionRepository = new TransactionRepositoryMock;
std::forward_list<...
-1
votes
1
answer
85
views
std::distance provide a past the end index when given an iterator returned by std::find
This is an exempt piece of code I'm currently working on.
std::find seems to do the job as expected, however std::distance always returns 3, i tried various configurations or there is something wrong ...
2
votes
0
answers
170
views
Problem with initializing std::<list> or std::<vector>
I've written a class where I use typedef to define a new kind of list and in private: I initialize/declare a vector. Neither the list nor the vector appears to be initiated despite the header files ...
1
vote
1
answer
86
views
couldn't deduce template parameter 'T'
I'm trying to make a logger lib by using source_location::current() as a default parameter when call assign function.
#include <iostream>
#include <source_location>
#include <...
0
votes
0
answers
50
views
How to serialize std::unordered_map with std::variant to binary file? [duplicate]
Trying to find a way how to serialize
std::unordered_map<std::string, std::variant<bool, int, float, std::string>> m_Settings{};
Is there any other way to serialize it other than going ...
-2
votes
2
answers
33
views
rdbuf in basic_ios and basic_fstream [closed]
I was checking the documentation for basic_ios which has the following methods:
but when I saw basic_fstream i found this:
AFAIK C++ cannot have two functions with the same name but different return ...
2
votes
2
answers
1k
views
Does GCC support C++23 std::print?
On CppCon 2022 was announced, that new official HelloWorld in C++ is now:
#include <print>
int main() {
std::print("Hello world\n");
return 0;
}
Do you know, is std::print ...