"A whole class for a simple function, obscuring syntax and adding complexity? If I didn't know you were being serious, I'd use that word you don't like."
It's called a "functor".
LB wrote:
"Both varieties give the same level of control"
Can you jump ahead or jump backwards in a RBF? No, so the level of control is in fact not the same -- less in fact, as I said previously.
LB wrote:
"I've been working with the traditional for loops for years now and they still look obscure."
Then you're doing something wrong. All of my loops are more readable because of my conventions.
LB wrote:
"but you have to think and figure out what it actually does."
typedef MyContainer_t::const_iterator It;
#define BEGIN MyContainer.begin()
#define END MyContainer.end()
for (It it = BEGIN; it != END; ++it)
{
// ...
}
L B wrote:
"A whole class for a simple function, obscuring syntax and adding complexity? If I didn't know you were being serious, I'd use that word you don't like."
Let's say I am creating templated function do_something_with_container(Container& T)
It should iterate from the beginning to the end.
I will ship it to the customer. He might use it with arrays.
How the hell will you use functor saving transparency of use and without using std::begin/end?
Another example I posted before: there is container you cannot change and which doesn't provide begin/end functions. You should make it work with your function without any code duplication and without changing function signature. What will you do in this case?
Things to note:
- I don't like using pre-made libraries (unless I'm the one who wrote it)
- I almost never use the Standard C++ Library; it's terrible.
- I like to work on the lowest-possible level
- I'm a narcissist, and I know it
Yeah, for those that don't know, C++14 is planned to be a bugfix release like C++03 was, and C++17 is planned to be the next major additions release of C++.
This problem was discovered, when a different paper was being prepared, proposing more then one dimension for std::array
If it will be supported for std::array, it will probably will be supported for std::dynarray.
Two dimensional dynamic arrays + some functions to manipulate them = matrix. It is close.
Also: does anybody uses valarray? It might be useful to implement vectors for example (in mathematical sense) or, using slice and gslice, dynamic multidimensional arrays, but it looks too obscure to use it directly.
I love valarrays because, in popular implementations, they give you expression templates without any boilerplate (try that in Java!), but when I need to do something with a matrix, I end up using a matrix library (boost.ublas, eigen, etc), for the functionality.
Also: does anybody uses valarray? It might be useful to implement vectors for example (in mathematical sense) or, using slice and gslice, dynamic multidimensional arrays, but it looks too obscure to use it directly.
I think I'm going to suggest std::valarray to beginners instead of std::list or std::vector, of course only when the usage scenario is appropriate.
As for me, I haven't had the chance (or desire) to use std::valarray yet, but it does look good. Maybe it can be used to manipulate OpenGL matrices? Then copy their contents back to a contiguous container such as std::vector?
Sarcasm aside, nope, just suck enough at programming that I don't see the point in learning more things I won't end up using in my code for lack of understanding it.
Let's say I am creating templated function do_something_with_container(Container& T)
That's probably why you don't see any standard C++ functions simply pass a container, the better option would be for the function to simply take iterators.
Using functors it would just be something like this:
1 2 3 4 5 6 7 8 9 10
template <typename C>
class BeginFunctor
{
public:
typename C::iterator operator() (C& c) const { return c.begin(); }
};
template <typename C, typename B = BeginFunctor<C>, typename E = EndFunctor<C>>
void do_something_with_container(C& T, const B& b = B(), const E& e = E());
Though std::begin/end would probably be the better choice to go as it is already provided and probably the better solution to supporting range based for loops than having a clashing naming convention in the container itself.
"That's the word you don't like?"
The word he doesn't like is "trolling" and it seems to be a word people jump to because they don't know how to handle a discussion disputing over some subject. If anything "stubborn" is a better suited word.
That's probably why you don't see any standard C++ functions simply pass a container
That actually because function taking two iterators is more general (you can apply it to only part of the container). I would love if standard algorithms would have overload taking container: std::sort(myVector, absoluteValue()); instead of std::sort(myVector.begin(), myVector.end(), absoluteValue());. We use helper function with these signatures and it really improves code readability.
Using functors it would just be something like this:
It should work with arrays transparently to customer. He shouldn't do anything to make it work with arrays.