How to enable 'auto loops' in custom classes? [duplicate] - c++

This question already has answers here:
How to make my custom type to work with "range-based for loops"?
(10 answers)
Closed 4 years ago.
I've written a class vecMatrix that wraps around std::vector to provide two-dimensional storage functionalities. However, while it's incredibly convenient to be able to write loops that sweep the data in an std::vector object as
std::vector<float> vec;
for (auto& val: vec) { /* do stuff to val*/}
I can't do it with my custom class. What kind of operator overload is required to be able to code the same way for vecMatrix?:
vecMatrix<float> mat;
for (auto& val: mat) { /* do stuff to val*/}

You'll need to define member functions begin and end that return iterators to the range that your class represents (or you can define non members that take a reference to your class as arguments.

Related

Argument passing of template class as an interface [duplicate]

This question already has answers here:
Templates polymorphism
(7 answers)
Closed 1 year ago.
Here is my scenario:
class Container {};
class Papa {};
class Child: public Papa {};
void myFunction(Container<Papa> container);
Now I want to pass two different parameters to myFunction().
int main()
{
Container<Papa> papaContainer;
Container<Child> childContainer;
myFunction(papaContainer);
myFunction(childContainer); // this line is not working
}
apparently I cannot do this.
no suitable user-defined conversion from "Container<Child>" to "Container<Papa>"
What would be the solution for this case? How Container<Papa> can be used as interface here?
This seems similar to this question here: No Suitable User Defined Conversion
although that one is using numeric types. Is there anything in myFunction, or that myFunction returns that would need to differentiate Child from Papa?

Good way to declare set of std::unique_ptr if I want it to be sorted by objects, NOT by pointers? [duplicate]

This question already has answers here:
Order of elements in set of pointers
(4 answers)
c++ custom compare function for std::sort()
(3 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I want to declare set of std::unique_ptr<A> like this:
class A
{
public:
///type of itself
typedef A self;
///Simple constructor
A(int w) : weight(w) {}
///To use for sorting
bool operator<(const self& another)
{
return weight < another.weight;
}
private:
int weight;
};//class A
//Container of pointers to A sorted by object, not by pointer values.
std::set(std::unique_ptr<A>, /*something*/) storage_for_A;
So, what to use in something? The obvious way is to declare a class for comparing two std::unique_ptr<A> by value of A and use it in above container declaration. However is there a better way? May be something predefined exists already? Surely, the problem is pretty standard.
Update: I know there is a question "Order of elements in set of pointers", but this is not what I am asking here. I know how set of pointers works. And I know about the solution, suggested there, which is mentioned in my question. I explicitly asked whether solution without custom-defined class for comparison is possible. E.g. through use of lambda functions?

Why overload the () operator (callable operator) in a C++ class or struct when you could use a constructor? [duplicate]

This question already has answers here:
Why use functors over functions?
(7 answers)
Closed 3 years ago.
Why would you overload the () operator in a C++ class or struct in C++11 or higher? As far as I can tell, these operators are overloaded when you want to pass objects like classes or structs into a std::thread and kick off a new thread with a package of data to go along with it, through a callable type.
But other than that, why else would you overload the () operator? Couldn't you simply do the same things in the constructor for a class or struct?
Why use
struct MyCallableStruct{
void operator() () {
dosomething();
}
}
when you could do
struct MyCallableStruct{
MyCallableStruct() { dosomething(); }
}
They're totally different.
First and most importantly, When you use operator(), it can be passed as function parameters (by object).
In contrast, when implemented by constructor, it can only be passed through templates (by class)
Second, operator() can be called several times after object created,
while construtor can only be called at constructing
All in all, they're different and useful in different scenarios

Can c++ cast vector<Derived*> to vector<Base*>? [duplicate]

This question already has answers here:
C++: How do I pass a container of derived classes to a function expecting a container of their base classes?
(4 answers)
C++: Can I cast a vector <derived_class> to a vector <base_class> during a function call?
(5 answers)
Closed 7 years ago.
I want to create a generic function which gets vector types as argument.
Derived1, Derived2, ... are derived (simply) from Base
I tried something like this:
int function (vector& foo)
It didn't work.
Could you tell me, what is the best generic solution for this problem?

Dynamically sized non resizable array [duplicate]

This question already has answers here:
Container of fixed dynamic size
(4 answers)
Closed 7 years ago.
Is there a C++ type that acts like a "dynamically sized non resizable array"? This kind of type can be thought of as one of two things:
vector<T> but without resize, push_back, etc.
array<T,N> but where N is dynamic and not static.
I do not want a solution that only works if the type of the elements within the array is a non-copyable type. I want a generic solution.
Yes, there (pretty much) is. std::unique_ptr<T[]>. The primary template has a partial specialisation for this case, which provides the appropriate interface (operator [], no operator * etc.)
Alternatively, you can wrap std::vector in your own class and restrict its interface. You could even do that by deriving a class from std::vector using non-public inheritance and publishing only the relevant parts of its interface:
template <class T, class A = std::allocator<T>>
struct FixedVector : private std::vector<T, A>
{
using FixedVector::vector::vector;
using FixedVector::vector::operator=;
using FixedVector::vector::get_allocator;
using FixedVector::vector::at;
using FixedVector::vector::front;
using FixedVector::vector::back;
using FixedVector::vector::data;
using FixedVector::vector::begin;
using FixedVector::vector::cbegin;
using FixedVector::vector::end;
using FixedVector::vector::cend;
using FixedVector::vector::empty;
using FixedVector::vector::size;
using FixedVector::vector::operator[];
};