online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. cpp_template_const_vector_of_smart_ptrs_test_AFTER Gabriel Staples www.ElectricRCAircraftGuy.com 10 May 2020 References: 1. https://stackoverflow.com/questions/61707217/no-matching-member-function-call-to-push-back-vector-of-shared-pointers/61707264#61707264 *******************************************************************************/ #include <cstdio> #include <iostream> #include <memory> #include <vector> template <typename T> class Container { private: // const std::vector<std::shared_ptr<T>> vec_; // does NOT work // Create an alias to this type just to make the creation below less // redundant in typing out the long type using vec_type = std::vector<std::shared_ptr<T>>; // NON-const object (vector)--so it can be changed vec_type vec_; // const pointer to NON-const object--so, vec_p_ can NOT be re-assigned to // point to a new vector, because it is `const`! But, **what it points to** // CAN be changed because it is NOT const! vec_type * const vec_p_ = &vec_; // This also does NOT work (in place of the line above) because it makes // the **contents of what you're pointing to const**, which means again // that the contents of the vector can NOT be modified. // const vec_type * const vec_p_ = &vec_; // does NOT work // Here's the compile-time error in gcc when compiling for C++17: // main.cpp: In instantiation of ‘void Container<T>::append(std::shared_ptr<_Tp>) [with T = std::basic_string<char>]’: // <span class="error_line" onclick="ide.gotoLine('main.cpp',78)">main.cpp:78:53</span>: required from here // main.cpp:61:9: error: passing ‘const vec_type {aka const std::vector >, std::allocator > > >}’ as ‘this’ argument discards qualifiers [-fpermissive] // vec_p_->push_back(std::move(item)); // ^~~~~~ // In file included from /usr/include/c++/7/vector:64:0, // from main.cpp:22: // /usr/include/c++/7/bits/stl_vector.h:953:7: note: in call to ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::shared_ptr >; _Alloc = std::allocator > >; std::vector<_Tp, _Alloc>::value_type = std::shared_ptr >]’ // push_back(value_type&& __x) // ^~~~~~~~~ // To prove that vec_p_ can NOT be re-assigned to point to a new vector, // watch this: vec_type vec2_; // vec_p_ = &vec2_; // COMPILE-TIME ERROR! Here is the error: // main.cpp:44:5: error: ‘vec_p_’ does not name a type; did you mean ‘vec_type’? // vec_p_ = &vec2_; // COMPILE-TIME ERROR! // ^~~~~~ // vec_type // BUT, this works just fine: vec_type * vec_p2_ = &vec2_; // non-const pointer to non-const data public: void append(std::shared_ptr<T> item) { vec_p_->push_back(std::move(item)); } void printElements() { for (int i = 0; i < vec_p_->size(); i++) { // Notice we have to use a double de-reference here now! std::cout << *(*vec_p_)[i] << std::endl; } } }; int main(int argc, char** argv) { std::unique_ptr<Container<std::string>> c = std::make_unique<Container<std::string>>(); c->append(std::make_shared<std::string>("hello")); c->append(std::make_shared<std::string>("world")); c->printElements(); return 0; } /* Output: hello world */

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue