Questions tagged [c++11]

29075 questions
8

votes
3

answer
307

Views

Move constructor for returned objects breaks C++98 code?

The Standard doesn't not require a compiler to perform return-value-optimization(RVO), but then, since C++11, the result must be moved. It looks as if, this might introduce UB to/break code, which was valid in C++98. For example: #include #include typedef std::vector Vec; struct Manager{ Vec& vec...
ead
1

votes
2

answer
40

Views

How do i specialize my template for std::string

I have the following templated function template ::type* = nullptr > As getStringAs(const std::string& arg_name) { std::istringstream istr(arg_name); As val; istr >> val; if (istr.fail()) throw std::invalid_argument(arg_name); return val; } And i would like to use it like this: getStringAs('2.f')...
eucristian
1

votes
1

answer
60

Views

How to access map of set of pairs elements?

#include #include #include #include int main() { std::mapmap1; for(int i = 0; i != 3; ++i) map1[i].insert({i+1,i+2}); for(auto i : map1){ std::cout
Aditya Ishan
1

votes
1

answer
32

Views

C++ Universal Reference and type deduction of LRef parameter

Assume we have some code template void Foo(T&& param); int x = 5; // deduced Foo(int&). // Argument type A = int. T = int&. Foo(x); int& rx = x; // deduced Foo(int& &&) -> Foo(int&). // But... argument type A = int&, an lvalue reference to int. // Is T = int& or under the hood it is T = int& &...
1

votes
1

answer
42

Views

Conversion type error when chaining method

I'm lost on this piece of c++ code that involves iterating over a list contained in a struct RouteInfo. How come the code shows this compilation error: Severity Code Description Project File Line Suppression State Error (active) E0312 no suitable user-defined conversion from 'std::_List_...
st4rgut
1

votes
1

answer
76

Views

Is there good practices for where to declare using in C++? [closed]

Is there good pratices to declare a using (for typedef like usage) ? For example if I have a class with a std::array member and I want to ease both reading and writing with a using like this template using matrix = std::array; Where should I put this ? Inside the class declaration or outside or even...
Pezzza
1

votes
2

answer
36

Views

How to add to a collection which is already stored in a collection?

I have the following: class SpritesheetManager{ std::unordered_map _loadedFiles; void addFileToFileListForSheet(std::string sheetprefix,std::string filename); } When adding files I do this: void SpritesheetManager::addFileToFileListForSheet(std::string sheetprefix,std::string filename){ bool contain...
Kaizer Sozay
1

votes
2

answer
57

Views

Definition and Initialization of a tuple whose components are of the same templated class, but with different specialisations

I am new to c++ metaprogramming. I tried to look at other answers, but I was not able to find one that could suit my problem. Or simply I was not able to apply it to my case. Here I will post a simplified version of the code, to highlight the main features which I would like to obtain. What I would...
Garo
1

votes
1

answer
46

Views

Error in default function template arguments when trying to convert C++11 code to C++03

I am trying to convert C++11 code to C++03 and stuck on a default template argument. #include #include #include #include #include template struct enable_if {}; template struct enable_if { typedef T type; }; template struct is_char { typedef typename enable_if::type eif; }; template struct cond...
1

votes
1

answer
59

Views

why can't the compiler bind a const int to an rvalue reference?

Here is a subset of my code. Why can't it bind a const int to an rvalue reference? What I expect is that T will be deduced as int const & and the type of e as int const & && will be folded to int const &. template List::List(initializer_list il) { for (T const & i : il) insertAsLast(i); } template...
BAKE ZQ
1

votes
1

answer
62

Views

Do mutable lambdas have their own copies of captured values?

std::function create_function (args...) { int x = initial_value (args...); return [x] () mutable { std::cout
spraff
1

votes
1

answer
23

Views

Initializing a vector<std::string> in uniform initialization

I am using the following code class test { public: test(std::vector str) { auto a = str[0]; a = 'B'; } test() { } const std::multimap _var= { {0x01, { { 0x0f, std::vector{'A', 'B', 'C', 'D'} } } } }; }; int main() { test t; std::cout
Rajeshwar
0

votes
0

answer
15

Views

How to fix error “vector iterators in range are from different containers” in c++?

I don't know where is the problem. The program crashes in this function. Can you help me? void atrinkimas_2(vector & studentai, vector &silpni) { sort(studentai.begin(), studentai.end(), tikrinimas_gal); std::vector::iterator it = std::find_if(studentai.begin(), studentai.end(), tikrinimas_5); std::...
22

votes
3

answer
800

Views

std::is_constructible on incomplete types

I have the following code: #include class A; int main() { std::cout
R_Kapp
1

votes
2

answer
40

Views

Passing an initializer list to a vector based constructor

I currently have the following code class test { public: test(std::vector str) { } test() { } const std::multimap _var= { {0x01, { { 0x0f, {'A', 'B', 'C', 'D'} } } } }; }; int main() { test t; } Error: main.cpp:29:9: error: could not convert '{{1, {{15, {'A', 'B', 'C', 'D'}}}}}' from '' to 'co...
Rajeshwar
1

votes
0

answer
24

Views

Why does my compiler make me add the “this->” pointer to access parent variables from a child class [duplicate]

This question already has an answer here: Why do I have to access template base class members through the this pointer? 3 answers Derived template-class access to base-class member-data 3 answers I'm coding a simple LinkedList class that is a child class to an abstract List class. After finishing...
jkulskis
0

votes
0

answer
17

Views

Lambdas, Is this keyword needed to disambiguate const/non-const member functions?

When I use MSVC to compile this program, I receive the error, 'Example::bar ambiguous call to overloaded function' Then, I found that the this keyword was able to resolve the error. Surprised, I used rextester and found that both Clang and GCC were able to compile the program without the this keywo...
Rabster
2

votes
1

answer
36

Views

Why does std::is_nothrow_move_constructible return false with user-declared move constructor?

everyone. I'm trying to add noexcept specifications and static_asserts to my class. To accomplish this goal I've made a small research on how std::is_nothrow_move_constructible works. I've got strange results: std::is_nothrow_move_constructible::value for a class A with only default-constructor decl...
Arks
0

votes
1

answer
24

Views

Capture output from console app started by daemon

How is it possible to capture the output from a console app, if it's started by a daemon process in Ubuntu Server 18.04? For a non-daemon process I usually use this: string myexec(const char* cmd) { array buffer; string result = ''; unique_ptr pipe(popen(cmd, 'r'), pclose); if (!pipe) { result = '...
Miguel E
1

votes
1

answer
12

Views

construct std::pair in-place in vector::emplace_back

I have a class A defined like bellow: class A { public: A() = default; explicit A(uint32_t a, uint32_t b) { std::cout
Cricket
3

votes
1

answer
17

Views

std::is_constructible on incomplete type

I have the following code: #include class A; int main() { std::cout
R_Kapp
0

votes
1

answer
54

Views

Filling an std::vector with raw data

I need to fill a vector with raw data, sometimes 2 bytes, sometimes 8... I ended up with this template function: template void fillVector(std::vector& dest, T t) { auto ptr = reinterpret_cast(&t); dest.insert(dest.end(),ptr,ptr+sizeof(t)); } with this I can fill the vector like this: fillVector(dst...
ErniBrown
5

votes
3

answer
248

Views

Are non-static class members destroyed even without a destructor?

In Bjarne Stroustrup's 'The C++ Programming Language (4th edition)' in section 17.6 (Generating Default Operations) it mentions this: If the programmer declares a copy operation, a move operation, or a destructor for a class, no copy operation, move operation, or destructor is generated for that cla...
johnnyodonnell
1

votes
2

answer
456

Views

Getting different results using “cin” for Drill #7 in Chapter 4 of Programming: Principles and Practice using C++ (Stroustrup)

I'm busy working through the 2nd edition of Programming: Principles and Practice using C++ (Stroustrup), and getting a problem with reading in values with and without a space between them, then displaying them again. Can you please point me in the right direction so I can find out why the results di...
Wim Olivier
1

votes
2

answer
380

Views

error: 'const std::array<int, 3u>&' is not a class, struct, or union type

I am trying to make a dump for most of the conteners but I got a problem when I put put a contener in value of my map. there is my code: #include #include #include #include #include #include #include template struct has_const_iterator { private: typedef char yes; typedef...
Gabriel de Grimouard
1

votes
1

answer
5.5k

Views

“is private within this context” is being thrown for function which should not be private (GCC 5.3.0, C++11)

I am attempting to create a queue, which requires the creation of another object stored in the queue. The errors are binary.cpp: In function ‘int main()’: binary.cpp:183:1: error: ‘Queue::Queue(T) [with T = binary*]’ is private Queue::Queue(T item){ ^ binary.cpp:286:65: error: within this co...
ceaston7
1

votes
2

answer
150

Views

Why doesn't the scope resolution not go outside of the class?

I am trying to write a generic reverse wrapper for containers that use bidirectional iterators using std::reverse_iterator. However, it would seem that when the compiler looks for begin(...) or end(...), it'll say it can't find a matching function call to reverse_wrapper::begin(container) because th...
Adrian
1

votes
3

answer
1k

Views

Making std::to_string work with void pointers, like operator<<? [closed]

I have some code which std::to_string()'s a variable whose type is a template parameter. Suppose the code is: template std::string foo(const T& t) { return std::string('I got ') + std::to_string(t); } Now, sometimes this parameter needs to be a void *; and I would like to get the address in the...
einpoklum
1

votes
2

answer
2.2k

Views

shared_ptr custom allocator together with custom deleter

Is it possible to use both a custom allocator and a custom deleter at the same time for std::shared_ptr? It seems to me that there is no way of doing so, since std::allocate_shared doesn't take a deleter. And also, the only sensible signature of the deleter would be something like void deleter(T*, c...
Zizheng Tai
-3

votes
0

answer
13

Views

How to apply c++ boost pagerank algorithm for non-distributed graphs?

A distributed graph is whose vertices & edges are distributed across multiple processes or address spaces.
kranti sairam
1

votes
1

answer
3.1k

Views

C++11 activation with <target_compile_feature> or <set (CMAKE_CXX_STANDARD)>

I'm using a Python library, named PyPHS, specialized in physical modeling. To save computation during the simulation, it implements a C++ code generation feature. It uses CMake to generate an executable of a particular simulation. It is implemented in C++ 11. Issue In the CMakeLists.txt file, the C+...
Vic Tor
1

votes
1

answer
86

Views

c++ add data to a std::vector containing a std::tuple

Below is code that adds elements to a vector containing a std::pair std::vector args_; template inline OperationEntry& setArg(const std::string& name) { args_.push_back({name, typeid(T)}); return *this; } How do I add elements to a vector containing a std::tuple? std::vector args_; template inline...
cherry aldi
1

votes
1

answer
77

Views

Can lifetime of objects in a initializer list be extended?

I have the impression that std::initializer_list might behave like literal strings in C++, and even further they might extend the lifetime of const references. Is that a correct assessment? Can objects in an initializer_list be somehow referenced later (without copying them), in local scope? in glo...
alfC
1

votes
2

answer
97

Views

Is there a way to automatically resolve an overloaded method via a template parameter?

I was trying to write a general purpose method locking wrapper for operations around a list. What I currently have is: template auto locked_call (OP op, ARGS... args) const -> decltype(op(args...)) { std::lock_guard g(lock_); return op(args...); } And, I can use it like this: auto push_back = [this...
jxh
1

votes
1

answer
115

Views

warning: offset of on non-standard-layout type 'DerivedClass'

I need to get rid of this warning. As far as I understand, it appears because DerivedClass is not considered as Plain Old Data. I read cppreference about POD and Standard Layout Type but I still do not understand why DerivedClass is not a POD. If I put members of BaseClass into DerivedClass and do n...
chm
1

votes
2

answer
61

Views

Can a class object be created as an lvalue-only?

A well-known problem with std::lock_guard (and its relatives) is that it does not work the way as expected when only a temporary object is created. For example: std::mutex mtx; std::lock_guard {mtx} // temporary object, does not lock the entire scope std::lock_guard lck{mtx} // correct I tried refer...
Tootsie
1

votes
2

answer
28

Views

scope block when use std::async in function other than the main function

I have some problem with st::async when is use this in other function other than Main function, suppose, I have functions like flowing : void printData() { for (size_t i = 0; i < 5; i++) { std::cout
Mr.DeveloperCplus
1

votes
1

answer
70

Views

Refactoring where unique functions have different arguments

I have a class (acting as a wrapper over a library) with member functions that all follow this pattern: MyObject& MyObject::ObjectFunction(int arg1, std::string arg2) { LibraryObject destination_object; libraryFunction(destination_object, arg1, arg2); setProp(destination_object); ~destination_object...
Shan
1

votes
2

answer
50

Views

Different behavior across compilers for std::enable_if (dependent on Outer class template parameters)

I have a nested (Inner) class, for which I want to enable_if a constructor, depending on how many template parameters (Args) the enclosing class (Outer) has. I came up with the code below, only to find out that it compiles fine on some compilers, and on some not. #include #include template struct...
sthlm58
1

votes
2

answer
72

Views

optimisation advice on value clamping in a loop

I have a tight loop exactly like what Chandler Carruth presented in CPP CON 2017: https://www.youtube.com/watch?v=2EWejmkKlxs at 25 mins in this video, there is a loop like this: for (int& i:v) i = i>255?255:i; where v is a vector. This is exactly the same code used in my program which after profili...
james

View additional questions