No warning message generated by g++ when using [[nodiscard]] and compile with lower stl option

is there nodiscard attribute available in c++11? If not, why no warning message output by g++ code belows. //test.cpp #include<iostream> [[nodiscard]] int foo() { return 1; } int main() { std::cout<<foo()<<std::endl; return 0; } At the beginning, i complie the program by "g++ -std=c++20 test.cpp -o test.o" and everythings is fine. But then i want… Read More No warning message generated by g++ when using [[nodiscard]] and compile with lower stl option

How to apply function that returns Result to each element of HashSet in Rust

As a language that aims for safety and performance, Rust provides a powerful data structure called HashSet that provides a fast and efficient way to store and retrieve unique values. HashSet is optimized for scenarios where you need to check for the presence of a value and avoid duplicates, making it a popular choice among… Read More How to apply function that returns Result to each element of HashSet in Rust

why can I use a class as custom comparison criterion as a template argument to std::map without providing a class instance

I’m reading the std::map part of The C++ Standard Library by Nicolai M. Josuttis. Here is a simplified code which can show the question bother me a lot: #include <iostream> #include <map> #include <string> class MyCompare { public: bool operator() (const std::string& lhs, const std::string& rhs) const { return lhs < rhs; } }; int… Read More why can I use a class as custom comparison criterion as a template argument to std::map without providing a class instance

Return the elements in an array using operator overloading with a class passing through it

I want to create a dynamic memory for a car dealership for bikes and cars but the vector didn’t print the right things and has lots of repetitions that aren’t supposed to be there. I created an object for the overlapping variables for both vehicles so I can pass it through the bike class later… Read More Return the elements in an array using operator overloading with a class passing through it

std::equal not working when using vectors returned by a getter

While learning about how std::equal works, I was writing some test code and ran into this problem: #include <iostream> #include <vector> #include <algorithm> class Foo { public: std::vector<int> vec; Foo(std::vector<int> _vec) { vec = _vec; } std::vector<int> get() { return vec; } }; int main() { std::vector<int> x{ 1, 2, 3, 4, 5 }; std::vector<int>… Read More std::equal not working when using vectors returned by a getter

How to use comparer function inside class with algorithm header?

This is what my compare function looks like: bool smallest_weight(const size_t& i, const size_t& j) { return this->abs_weight[i] < this->abs_weight[j]; } I use this function inside the constructor of the class to initialize some other arrays. This is the code that uses it: size_t best_node = *min_element( this->pointer_list[i + 1].begin(), this->pointer_list[i + 1].end(), smallest_weight );… Read More How to use comparer function inside class with algorithm header?