8,580 questions
2
votes
4
answers
237
views
Two pointers referencing the same memory location, possible to make null if we deallocate the space?
If two pointers are referencing the same memory location. Will it be possible to make one pointer null, if we deallocate that memory location?
For example:
#include <iostream>
using namespace ...
1
vote
4
answers
119
views
Setting std::function callback in polymorphic type
We are trying to create a class for setting options that can be either float or int (in reality, there are several more types). We want to store options of any kind in a single list, so we have made a ...
3
votes
5
answers
294
views
Does unsequenced behavior repeat itself?
tl;dr
I have a program that "works", but does so in part via unsequenced behavior. I recognize that code with unsequenced behavior can technically do anything - however, it has to do ...
-3
votes
2
answers
183
views
Deleting memory for containers
I have std::map of std::vector of raw pointers.
According to Google AI, in order to clean it I should do:
int main() {
std::map<int, std::vector<MyObject*>> myMap;
// Populate the ...
0
votes
0
answers
46
views
Does QStandardItemModel take ownership of items when using appendRow()?
In my Qt application, I'm dynamically creating QStandardItem objects and adding them to a QStandardItemModel using appendRow():
auto* item = new QStandardItem("text");
model->appendRow(...
0
votes
0
answers
58
views
Pass a variadic templated function taking universal references as pointer to a utility
I have a problem with an attempt at factorizing two utility functions, each calling a given "hardcoded" function, into one utility with a functor call instead. Meaning I have a factoring ...
2
votes
4
answers
198
views
A generic wrapper for C++ vector of structs operations: removal, find_if, etc
I'm using C++ 14. I find C++'s vector operations erase and remove verbose and confusing and want to write a simple generic wrapper to remove an element from a vector of structs if that element's value ...
0
votes
4
answers
269
views
Is it possible to extend a member function?
I am relatively new to C++ and would like to know if it is possible to do the following and if so, is there a better way to achieve the same or similar result more efficiently?
Declare a struct/class
...
0
votes
1
answer
118
views
Dynamic list of arguments for a variadic function
I need to refactor the following code (simplified for clarity) :
#include <iostream>
// those structs cannot be changed
struct Foo1 {int f;};
struct Foo2 {int g;};
struct Foo3 {int h;};
...
0
votes
4
answers
202
views
Prevent single member element from copy or moving?
I'm using a single structure to store multiple parameters. I want to access these parameters via their element names, just like a normal structure. I also want to name the structure elements and ...
4
votes
2
answers
130
views
Type trait to check whether a function can compile with a given type
I have two types, CustomNotCompatibleWithRun, CustomCompatibleWithRun, and a run() function:
#include <iostream>
#include <type_traits>
template <typename T_>
struct ...
-1
votes
3
answers
183
views
Why, if constructing a shared_ptr from raw pointer, "main()" exits with some negative error code? [duplicate]
Just experimenting with shared_ptr to explore its functionalities.
I'm constructing a new shared_ptr object using a raw pointer from other shared_ptr object.
The following code is compiling and runs ...
5
votes
1
answer
63
views
Can a boost::sml state machine with policies be copied?
From https://boost-ext.github.io/sml/examples.html#deferprocess
Modified to try and copy the fsm.
#include <boost/sml.hpp>
#include <cassert>
#include <deque>
#include <queue>
...
3
votes
1
answer
121
views
Template iterators of a specific value type
I am trying to create constructors for building a class, that can take iterators of anytype, as long as they are an iterator for a specific value_type. With a different handler for all iterators of ...
5
votes
1
answer
233
views
Can initialization of static constant be skipped by 'case' label?
I have a constant declared in one case of my switch statement:
void foo( int& v ) {
switch( v ) {
case 0:
static constexpr int c{ 0 };
break;
case 1:
v = c;
...
4
votes
1
answer
97
views
C++14: Inheriting constructors with "using" AND defining new constructors yields C2512 (no appropriate default constructor available)
In C++, you can inherit all constructors of a base class by writing using Baseclass::Baseclass
My Baseclass has both a default constructor and another one that takes an int argument, even though that ...
3
votes
2
answers
100
views
Restricting a Universal Reference to `const T&` or `T&&`
Is it possible to allow a universal reference argument to be only called as const lvalue-ref or rvalue-ref, but not lvalue-ref?
Consider this:
#include <string>
#include <iostream>
struct ...
3
votes
2
answers
146
views
Return content of optional
I have a function that returns a value from an internal optional.
T foo() {
std::optional<T> v;
...
return *v;
}
What's the most efficient way to return *v?
return *v
return *std::move(v)...
2
votes
1
answer
143
views
Using conversion operator with automatic return type deduction from base class
The following not very long program is treated differently by the current compilers:
class A {
protected:
operator auto();
};
template<class>
struct B : A {
using A::operator auto;
};
...
0
votes
1
answer
51
views
Overloading + Operator in C++ for Symbol Class and Avoiding Code Duplication
I am creating a binary tree representation of a mathematical expression for performing differentaiton and integration on it.
I am trying to overload the + operator for my Symbol class. It works fine ...
1
vote
4
answers
225
views
How to split std::array without copying?
I need to access an std::array<double, 9> x by two functions, namely funA and funB. funA only needs the first 6 entries of x and funB the last 3 entries. I am limited to C++14.
I know there are ...
0
votes
0
answers
122
views
Why did Stroustrup write in PPP (2nd editioln) that operator ++ does not work with type char?
I've read in Stroustrup PPP "Programming Principles and Practice Using C++", 2nd edition in table "useful operators for some common and useful types" here
that type "char&...
0
votes
0
answers
120
views
UB with reinterpret_cast
Am i allowed to do this safely, assuming func is only ever called with &container_instance.bar?
struct foo_t { int val; };
struct bar_t { int val; };
struct container
{
foo_t foo;
bar_t ...
0
votes
0
answers
49
views
Is it possible to modify objects with non-local scope in constexpr function since c++14?
I read the book "C++ Concurrency in Action, 2nd Edition" (from Anthony Williams), and I'm not sure of one sentence in appendix A.4.1 :
"But that's about all you can do in C++11 - a ...
1
vote
1
answer
77
views
Volatile and optimization with different compilation units
Please have a look at this more or less standard example that i see people use when talking about the usage of volatile in the context of embedded c++ firmware development on a baremetal target:
// ...
1
vote
1
answer
108
views
C++14 constexpr function requirements on cppreference
cppreference says that, until C++14, a constexpr function must satisfy the following requirement:
the function body must be either deleted or defaulted or contain only the following:
null statements ...
0
votes
2
answers
108
views
`std::sprintf` or `std::snprintf` with parameter pack without additional call and tmp buffer
Is there a way to get ride of the tmp and the second call to std::snprintf in Print? Can I do the same with just one std::snprintf call?
// arm-none-eabi-g++ -std=c++14 -O3 -Wall -fno-rtti -fno-...
2
votes
1
answer
143
views
Can private members be accessed by an outside alias template?
Clang accepts accessing data-member d from within alias
template p, whereas GCC and MSVC reject it. Should this be
allowed or not? What does the standard have to say about it?
When accessing d ...
0
votes
0
answers
38
views
how to specialize template using enable_if_t to accept int to enum conversion
I'm trying to write a specialized constructor that would accept integer argument and converted it to enum. My attempt is below, but it fails with an error. Other template candidates are rejected, and ...
3
votes
1
answer
198
views
Substituting `std::holds_alternative` in C++14 with mapbox variant
I'm currently downporting C++17 code to C++14. Since C++14 doesn't have variant I'm using the variant implementation by mapbox which is available here. Basically, substituting variant using mapbox::...
4
votes
2
answers
92
views
Partial template specialization for when all template parameters are the same type
Is it possible in C++14 (so no constraints, require or fold expressions, but there is SFINAE) to have a partial template specialization of a class template
template <typename ...T>
class Generic ...
0
votes
1
answer
212
views
Using is_XXX_v<> in C++14
I'm trying to port a C++17 project to an exotic platform that doesn't have anything newer than C++14. The C++17 project I'm trying to port to C++14 uses a lot of calls to is_XXX_v functions, e.g. ...
0
votes
0
answers
76
views
pybind11 bindings throwing exit code 139 (11:SIGSEGV) when accessing a struct member variable
I'm getting a segfault - Python exit code 139 (11:SIGSEGV) - when attempting to access a struct member variable. The struct looks like:
struct PlannerResult {
std::vector<Node> route;
double ...
1
vote
1
answer
124
views
memory_order_relaxed with c++11/14 in cppreference
I'm confused with this statement on Relaxed ordering
Even with relaxed memory model, out-of-thin-air values are not allowed
to circularly depend on their own computations, for example, with x
and y ...
1
vote
3
answers
155
views
c++ 14 Variadic Templates to call different methods with enum
I want to have 2 method, one is SetVariable and the other GetVariable in MyClass
both will receive an enum and arguments, the arguments can be more then one and more then one type
and according to the ...
2
votes
2
answers
99
views
Problem on SFINAE with `is_constructible` in constructor
I have a problem understanding why the following code fails to compile:
#include <iostream>
#include <vector>
#include <type_traits>
enum class Error
{
OK = 0,
NOK = 1
};
...
2
votes
1
answer
162
views
Does the standard specify when function template instantiation side-effects are visible? [duplicate]
Note that I am not asking "When do function templates get instantiated?" Rather, "When are the side-effects of that instantiation visible?"
An example of a side-effect of an ...
1
vote
2
answers
85
views
How to insert a key if not present to a transparently compared map?
I have a std::map<K, T, std::less<>> and a “key” of type KK which is order-compatible with K. If the key is present, I want to return the corresponding value. If the key is not present, I ...
0
votes
2
answers
110
views
How to binary search a std::vector BUT that return a RandomAccessIterator?
I'm looking for a log2(N) way of searching a std::vector and receive a RandomAccessIterator. I want it to work exactly as std::lower_bound() but return a RandomAccessIterator instead of a ...
0
votes
1
answer
155
views
Fixing boost::optional undefined type error
I was trying to create a bidirectional tree-like structure, so Ive ended up with the following struct`s:
template<typename T>
struct Node
{
T value;
std::vector<...
14
votes
3
answers
2k
views
Is it legal to initialize an array via a functor which takes the array itself as a parameter by reference?
In the question Idiom for initializing an std::array using a generator function taking the index?,which basically asks how one can initialize an array of an arbitrary type which is not necessarily ...
0
votes
0
answers
61
views
Why doesn't std::array<T,N>::operator[] return an rvalue reference for rvalue object? [duplicate]
std::array<T,N>::operator[] always returns an lvalue reference (const if needed), but what can possibly be the use of a line of code like this, which is valid C++?
std::array<int,1>{1}[0] =...
0
votes
0
answers
78
views
gRPC 1.47.4 vs2017 compilation errors
In my company we try to create packages of gRPC and all dependencies in our private conan repository. Currently we stuck with gcc6.4.0 and VS2017. I had currently created packages for these ...
2
votes
3
answers
264
views
In C++, compared to final or not virtual function, what is the advantage of CRTP?
I'm new to learning modern C++ programming. I have read many blogs and questions but haven't found the answer of this question. In C++, compared to final or not virtual function, what is the advantage ...
1
vote
2
answers
125
views
Specialize a method template with type parameter with a non-type one
I'm trying to implement a class template with static member functions that can handle different types. My code looks like this (very simplified example):
#include <array>
struct Foo {
...
5
votes
1
answer
396
views
Compile result different in GCC 13.1 and GCC 13.2 constructing a string
I have the following code that compiles fine on GCC 13.2. Colleagues can't compile it on GCC 13.1. The new version compiles fine even with -Wall -Wextra. I assume that GCC 13.2 is better and more ...
6
votes
4
answers
255
views
free() on result of placement new?
I'm trying to understand whether it's valid to call std::free() on the result of placement new in a buffer allocated by std::malloc().
Consider the following. Does this code exhibit any undefined ...
0
votes
0
answers
87
views
How to use boost log in visual studio 2015 and build platform is v140
I have added all the dependencies but after adding it i am getting some linking error. I am using visual studio 2015 buildtool v140 , platform v8.1 and platform win32.
I have added the necessary ...
1
vote
1
answer
114
views
How to print the line number of the caller function from a template function?
I am implementing a log function which logs along with line number of the code. The snippet of the code as follows:
#include <iostream>
using namespace std;
char m_buffer[500];
template<...
0
votes
2
answers
121
views
Seamlessly using maps with different comparators
I was using an alias to refer to one of two maps:
map<int, int> & map_alias = use_map_a ? map_a : map_b;
but then changed map_b to use std::greater as its comparator (instead of the default ...