Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
1 answer
27 views

Why is std::conditional reject T&?

I want to return void if template parameter is void, and return T& if template parameter is T. So, I write the next MRE code: #include<type_traits> template<class T> struct Foo { ...
chabapok's user avatar
  • 952
3 votes
1 answer
137 views

Unclear template resolution

I was going through the exercises in this post https://www.slamecka.cz/posts/2021-03-17-cpp-metaprogramming-exercises-1/ First of all, I want to say huge thanks to the author. The problems were quite ...
arsdever's user avatar
  • 1,262
4 votes
2 answers
141 views

Idiomatic ways of using tuples and std::tie

I am trying to implement the modular multiplicative inverse using the extended Euclidean algorithm in C++. In Python, the code for this is concise and idiomatic, using tuple unpacking: def inv_modulo(...
João Areias's user avatar
  • 1,418
4 votes
3 answers
278 views

Why does std::vector have 2 constructors instead of 1 with default argument?

I looked at cppreference.com and found this vector(); explicit vector( const Allocator& alloc ); why not just explicit vector(const Allocator& alloc = Allocator()); 1 constructor instead of ...
user24551355's user avatar
0 votes
1 answer
61 views

Is there a way to extract the search from a c++ regex? (not a question about regexs but #include <regex>)

given a C++ std regex is there a way to figure out what that regex would search for? get the string out that you put in when constructing it? I've seen https://en.cppreference.com/w/cpp/regex/...
Smasheded's user avatar
  • 116
2 votes
1 answer
88 views

How to merge/count adjacent 1's within an std:array using std?

Let say I've an std:array<int> such as: 0 1 1 1 0 1 1 0 I'd like to use std (if possible) to do this in c++11: sum adjacent 1's erase the summed value transform 0 to 1 So getting this result ...
markzzz's user avatar
  • 47.8k
0 votes
0 answers
38 views

Convert std::function to a function pointer

I have a function from the C library that accepts a function pointer int c_func(void(*func)(void*), void* data); I want to write a wrapper for this function that takes a std::function instead of a ...
Egor00f's user avatar
0 votes
1 answer
132 views

Why does an std::insert on an std::set of constant-sized arrays not work as expected?

I am in a scenario where I need to process a variable-sized array of constant-sized C-strings, namely: I want to know if each of those strings is unique. I tried using a std::set, as uniqueness of its ...
kamilok04's user avatar
1 vote
1 answer
264 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 ...
shichao's user avatar
  • 23
1 vote
1 answer
196 views

Why is the clear() function noexcept while the destructor not? [duplicate]

I noticed that, in the allocator-aware containers interface, the standard requires that the clear() member function be noexcept while the destructor not. Furthermore, it appears a discrepancy in the ...
LoS's user avatar
  • 1,794
1 vote
1 answer
84 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 ...
Igor Polk's user avatar
4 votes
2 answers
155 views

Is there a way to access arguments stored in the function object returned by std::bind() in C++?

I need a way to decompose the function object returned by std::bind() to its arguments in a function template. The code snippet below shows what I'd like to do: #include <iostream> #include <...
Armin Sobhani's user avatar
5 votes
5 answers
1k views

Why does std::array require the size as a template parameter and not as a constructor parameter?

There are many design issues I have found with this, particularly with passing std::array<> to functions. Basically, when you initialize std::array, it takes in two template parameters, <...
ruff's user avatar
  • 71
1 vote
1 answer
146 views

efficient use of std::move to aggregate all object instances in a std container

I need to centrally accumulate certain entity creations in my program into a container and I want to use the efficiency of std::move with a move constructor to aggregate all entities created anwhere ...
sith's user avatar
  • 530
0 votes
0 answers
29 views

asyncF.cpp:24:31: error: no member named 'async' in namespace 'std'; [duplicate]

I am compiling my code with clang++ and this gives me an error saying asyncF.cpp:24:31: error: no member named 'async' in namespace 'std' Here's my code // c++ program that demonstrates async // ...
CodeLect's user avatar
1 vote
2 answers
961 views

how does std::vector deal with the call to destructor?

When tring to implement std::vector, I get confused with the implicit call to destructor. Then element in vector maybe: T a Class Object, T*, shared_ptr<T> a Smart/Simple Pointer to Class ...
sixsixqaq's user avatar
1 vote
2 answers
340 views

To transform std::bind to std::function?

See the code below queue<function<void()> > tasks; void add_job(function<void(void*)> func, void* arg) { function<void()> f = bind(func, arg)(); tasks.push( f ); } ...
Torch's user avatar
  • 13
0 votes
3 answers
251 views

How do I create an std::array of immutable structs? I.e. structs with only const values [duplicate]

Say I have struct S: struct S { const int i; const bool b; } And I want to create an (non-const) array of this struct like so: std::array<S, 16> arr; for(int i = 0; i<arr.size(); i++) ...
Typhaon's user avatar
  • 1,044
0 votes
1 answer
113 views

How to cast struct of std:array<char> to single std::array<char>

I'm packing messages to be send to another device. I'm using C++11. Usually the payload is a single ASCII encoded value, but this one command has a bit more parameters. I'm trying to pack the payload ...
Typhaon's user avatar
  • 1,044
0 votes
1 answer
468 views

Showing the unlock from std::condition_variable::wait

I've read from https://en.cppreference.com/w/cpp/thread/condition_variable/wait that wait() "Atomically unlocks lock". How do I see this via std::cout? I am trying to understand conditional ...
davidj361's user avatar
  • 191
1 vote
1 answer
134 views

How can std::reference_wrapper<int> use operator+= if std::reference_wrapper doesn't have operator+=?

Thank you all, I didn't even know about user-defined conversion function and how it works. Why is it possible to use std::reference_wrapper<int>::operator+=, if such an operator does not exist, ...
Jonathan's user avatar
0 votes
0 answers
37 views

Correct way to pass std::vector from class to external function [duplicate]

I am attempting to pass a std::vector of a custom class datatype to an function external to the main class. However, I am not sure of the best way to do this. Currently it produces segmentation faults....
huss987's user avatar
0 votes
0 answers
115 views

What does the syntax struct C{int x;}var; mean in C++ [duplicate]

I am looking at the documentation about std::for_each. (https://cplusplus.com/reference/algorithm/for_each/) I cannot understand the way they are using the struct myclass because they are not ...
ashamat's user avatar
0 votes
2 answers
92 views

Initializing a 2-D array in C++14 [duplicate]

recently I've started to study C++'s standard library containers from the Deitel&Deitel book. Conceptually, it is quite clear to me, but I have a problem when trying to replicate a piece of code ...
FraTsuna's user avatar
1 vote
1 answer
939 views

What is the type of map's iterator?

I am new to C++. I found that to see a variable's type, I can use typeid().name() on library std::typeinfo. But when I implement this function on map data structure, I got this output Type of itr is : ...
emirhan demir's user avatar
0 votes
1 answer
288 views

C++ algorithm find_if in map

I want to find pair in map and just print it. This is my code: std::map<std::string, std::string> mLoginPasswordMap{ "test", "test2" }; std::string key1("test&...
L. Kicki's user avatar
1 vote
1 answer
64 views

Is it legal to derive from std::initializer_list?

Can I use std::initializer_list list as a base class? template <typename T> struct il : std::initializer_list<T> { using base = std::initializer_list<T>; il(base list): base(...
anton_rh's user avatar
  • 9,095
1 vote
2 answers
550 views

Why is std::move generating instructions here?

I heard time and again that std::move(t) is more or less only a fancy way of saying static_cast<T&&>(t) and would not generate any instructions. When I was playing around now with std::...
Elmar Zander's user avatar
  • 1,496
7 votes
1 answer
821 views

Indirect & Direct initialization of std::atomic in C++11/17. What are the differences?

On this CPP Con 2017 webinar, Fedor Pikus says: "it has to be direct initialization" This is the link to the webinar. What are the differences between these initialization methods? (and ...
Amit's user avatar
  • 1,130
0 votes
2 answers
795 views

Stopping multiple threads at once

What do I miss in the program below with threads waiting for a condition_variable_any to determine when to stop ? In the program listed below, the threads stop in an impredictable way; some before the ...
Less White's user avatar
0 votes
0 answers
562 views

How to instantiate a future created by std::async in a member variable?

What do I do wrong when I try below to add the future returned by std::async into a member variable? class PrinterDriver { std::vector<std::future<void>> m_PendingFutures; public: ...
Less White's user avatar
0 votes
1 answer
548 views

Why unique_ptr's Deleter need an argument of type unique_ptr<T, Deleter>::pointer?

I see the describtion about unique_ptr on cppreference, it says Deleter must be FunctionObject or lvalue reference to a FunctionObject or lvalue reference to function, callable with an argument of ...
asele's user avatar
  • 52
0 votes
1 answer
210 views

"pair" class made from "std::tuple"

I know that "std::pair" struct does not inherit from "std::tuple" class. However, I wanted to make "pair" class which inherited from "std::tuple" class. Is this ...
rin's user avatar
  • 11
0 votes
0 answers
70 views

find_if to seach abstract object list with std::string

I have a list which contains abstract objects and now I need to use a string search to the list to compare object's name and the string. Here is the abstract class. class ShaderEffect { std::string ...
cHiWa's user avatar
  • 433
4 votes
4 answers
853 views

Should a boolean predicate for std::condition_variable be volatile in C++?

I've been hearing so many conflicting answers, and now I don't know what to think. The agreed-upon knowledge is that for sharing memory in a thread safe manner in C++, it's required to use volatile ...
Ronen's user avatar
  • 301
0 votes
1 answer
636 views

How to pass reference type to std::hash

I am creating a Template Cache library in C++-11 where I want to hash the keys. I want to use default std::hash for primitive/pre-defined types like int, std::string, etc. and user-defined hash ...
AbhayKrSomani's user avatar
1 vote
1 answer
115 views

Is std::lower_bound free from race conditions

Is it safe to run std::lower_bound on a shared vector object in an OpenMP loop? (using C++11) Here is a basic example (edited from my code, not sure if it actually runs or not) // fill range std::...
Alex's user avatar
  • 297
0 votes
2 answers
162 views

Error while concatenating a vector to itself in c++

I am simply trying to concatenate a vector to itself but the following code is not working and I am not able to find the issue. If my input vector is {1,2,1}, the o/p I am getting is {1,2,1,1,16842944,...
Surbhi Jain's user avatar
3 votes
1 answer
175 views

Is there a way to delete from a set and move the value to a temporary?

Is there a way to delete an item in a set / map and get the former value as a temporary return value so that I can move() somewhere else ?
Bonita Montero's user avatar
0 votes
2 answers
531 views

std:atomic initialized with reference creates copy of underlying variable?

Let's consider next snippet: int val=5; int& ref=val; std::atomic<int> atomicref(ref); ++atomicref; std::cout<< "atomic ref="<<atomicref.load()<<" original ...
BorisV's user avatar
  • 689
5 votes
2 answers
499 views

Move a std::map vs move all the elements of a std::map

std::map<int, Obj> mp; // insert elements into mp // case 1 std::map<int, Obj> mp2; mp2 = std::move(mp); // case 2 std::map<int, Obj> mp3; std::move(std::begin(mp), std::end(mp), ...
Yves's user avatar
  • 12.2k
2 votes
3 answers
4k views

std::vector and move semantics

To enable move semantics on a std::vector do I need to pass by value; so the compiler will not copy the element but just move them? Example: class data { public: void setMyData(vector<string>...
user1583007's user avatar
1 vote
1 answer
105 views

Why should we use std name space before functions of <algorithm> header and should not use it before functions of <cmath> header?

I was wondering why we should use the std namespace before function of <algorithm> header like max() and we are not obliged to use it before functions of <cmath> header like round()? ...
HSA's user avatar
  • 13
0 votes
1 answer
563 views

Using find_if with a vector of pointers: How to pass pointer by const reference to lambda?

In the following code I try to compare a vector of pointers via find_if and determine which contains a member a == 5 (in this case both of course, but it shows my case). However it doesn't compile. #...
glades's user avatar
  • 4,660
0 votes
2 answers
565 views

Is it possible to place a std::array at a fixed memory address?

For an embedded design I want to place a C++ std::array at a specific memory address, which points to a buffer shared by hardware and software. Is this possible?
DavidA's user avatar
  • 2,359
2 votes
1 answer
148 views

How to delete the selected random data in std::vector?

How to delete the selected random data in vector? #include <iostream> #include <vector> int main() { std::vector<int> shuf{ 1, 2, 3, 4, 5 }; int random = shuf[rand() % shuf....
woofMaranon's user avatar
1 vote
0 answers
60 views

Segmentation fault when applying std::sort to a vector of vectors of integers [duplicate]

I am writing a struct for sorting generic data types allowing for multiple values of the same in each object to be sorted (e.g. sorting a vector of vectors of integers). The code below is a simplified ...
niran90's user avatar
  • 288
1 vote
2 answers
211 views

How to make std::map::find function case sensitive?

I had interviewed with a MNC company. He gave me the following code and asked me to make find() function as case-sensitive. I tried, but failed to understand how to make inbuilt find function as case-...
Jayesh's user avatar
  • 4,893
12 votes
2 answers
781 views

Why is unlocking an unlocked std::mutex UB?

Unlocking a std::mutex that wasn't locked is UB. Why is this so? Why doesn't it just have no effect, as the mutex isn't locked yet, or was already unlocked, so what's the harm of calling unlock again?
Random's user avatar
  • 249
0 votes
3 answers
450 views

C++ Conditional typedef with more than two type

I am trying to do something like this . if (flow1) { typedef template_class<user_defined_type1> x; } else if (flow2) { typedef template_class<user_defined_type2> x; } ...
spooja__'s user avatar
  • 223

1
2 3 4 5
21