klimi
this is wrong code.
Oh I will search other... Wait
klimi
this is wrong code.
Chiranjeevi Kodati: How to create function object in c++ for generic datatype. https://pastebin.com/sBYd9ZCR. Please resolve the issue in the code sample.
Ilya
dont ever do this
Yes I agree... But for student projects it is acceptable
...
Yes I agree... But for student projects it is acceptable
nope, you shouldnt be teaching them stuff that is not okay in somewhat larger projects
Chiranjeevi
this is wrong code.
Yes, can you correct that, i want to create a template function object
Ilya
Yes, can you correct that, i want to create a template function object
No, you don't want to create a template function. You want to use lambdas.
Chiranjeevi
I agree u have given a better alternative, but can we create function template object?
Javi
For which you need to include functional
You also have the scope wrong. It should be typename std::unary_function<T, T>::result_type
Ilya
You also have the scope wrong. It should be typename std::unary_function<T, T>::result_type
unary function is also not needed actually, and never was needed. The code would work brilliantly without those unary|binary|mem function.
Javi
Also, in general, I try to avoid auto. It is usually a source of problems. Not using it, forces you to think about what you are doing, and if the code changes, it will be less likely to keep running the wrong way
Javi
Or the code requirements make templates more convenient
Javi
Everything has its place
Chiranjeevi
You don't need to anymore, forget about them
Ok thanks, can you suggest good c++ book for modern practices standpoint
Javi
Don't
Don't what?
Ilya
Don't what?
Don't avoid AUTO
Javi
I would if I can
Javi
Very few circumstances need it
Ilya
I would if I can
No, AUTO is fully transparent and remove tavtology because it needs initializer and the type is completely cleanly seen from it
András
Very few circumstances need it
Its match easier to write auto instead of everything (except of int). For example vector<int>::iterator
Francisco
No, that is part of unary_function structure in c++
I'm not 100% sure, but I think this is deprecated
Javi
No, AUTO is fully transparent and remove tavtology because it needs initializer and the type is completely cleanly seen from it
Not transparent at all. If at a given point of the development the variable declared with auto changes the type, auto will adapt it and, if some other part of the code expects the old type, you are in trouble. It may be more convenient in some cases like the one mentioned by @András, but you risk breaking the code if you aren't careful with changes. I prefer to spend 0.5 seconds more writing vector<int>::iterator and save hours trying to find out why my code broke when I changed vector<int> by vector<something_else>
Francisco
Variables can't change their types
Javi
Its match easier to write auto instead of everything (except of int). For example vector<int>::iterator
It's easier and riskier. vector<int>::iterator is a random access iterator. If now you change your container to one whose iterator isn't random access, and you used this feature somewhere else, you'll get an error in a different point, and it will be more difficult to find out the problem
Javi
Variables can't change their types
Variables can't. You can refactor your code and decide that a variable is better suited with a different type
Francisco
Variables can't. You can refactor your code and decide that a variable is better suited with a different type
Yep, and a refactoring comes with a cost, and I'm pretty sure cost will be less if you use auto
Javi
But compiler at the compile time know which type should auto be, so If u change type radical, then you catch compile error
The compiler will replace auto with whatever it fits. And the error will happen somewhere else. Imagine that the line is auto var=myfunction(); Now I refactor and myfunction returns a different type. You won't get an error here. You'll get it somewhere else if you are lucky. If not, your program will simply give you wrong results
Javi
Compile time error
At a different place. And this is a case where you are lucky to get a compile error. Imagine that you change vector<double> b y vector<int>. And somewhere else in your code you have something like
Javi
double var=v[i]/23;
Javi
It will compile and run without problems
Javi
but var won't have the value you expect
András
Javi
Unsafe code
It's a silly example. If you prefer, you can have double var=v[i]/v[i+1];
Javi
Error like compiler cant do that with double types
You don't get a compiler error, that's my point
András
It's a silly example. If you prefer, you can have double var=v[i]/v[i+1];
Anyway, if u try to store int to double, it's unsafe
András
So u have to cast it
Javi
Anyway, if u try to store int to double, it's unsafe
You weren't doing it. That's my point. The vector was double and you changed it to int and forgot that you were using it there
Javi
If you change the return type of a function, the auto is probably the less important thing you should worry about
During development, it can happen. And anyway, why adding another problem? Just spend 0.5 seconds writing the type explicitly and save trouble
Francisco
During development, it can happen. And anyway, why adding another problem? Just spend 0.5 seconds writing the type explicitly and save trouble
I don't feel like writing std::map<std::string, int>::const_iterator everytime I do a begin or end, when I already know the type and the compiler can deduce it for me
Francisco
I find it a waste of time and leads to less readable code
Javi
Use a typedef
Francisco
Well, you shouldn't use typedef neither
Javi
I find it a waste of time and leads to less readable code
I don't think it's less readable. Actually it is more readable. What type is auto var=myfunction();? Now you have to look for myfunction() and see what it returns
Javi
If the type is explicit, you don't have to look anywhere. That line is clear as water
Francisco
If the type is explicit, you don't have to look anywhere. That line is clear as water
If you do a begin, it's clear water you're assinging an iterator
Javi
If you do a begin, it's clear water you're assinging an iterator
A normal iterator, a const iterator, a forward iterator, a backward iterator...?
Javi
Is the iterator random access?
Javi
begin can be for a custom class. What kind of iterator does it use when you call begin?
Javi
And it's not only iterator. Think of the example I gave some comments ago:
Javi
auto var=myfunction();
Javi
var=v[i]/v[i+1];
Javi
Sorry: auto v=myfunction();
Javi
auto var=v[i]/v[i+1];
Javi
No idea what var is
Javi
Or if I can use [] with v
Javi
I don't say never use auto, but if you can avoid it. I think it makes the code clearer and less prone to errors not to use it
Francisco
I don't say never use auto, but if you can avoid it. I think it makes the code clearer and less prone to errors not to use it
I don't agree. I think you should Almost Always Auto, and most of the commitee and C++ experts think the same way
Francisco
Makes refactoring a lot easier, less verbose, less error-prone
Francisco
Also, if the naming in your codebase is good enough, you can deduce the type just from the name. A getsomethingdate() function is pretty clear what it's returning
Javi
Makes refactoring a lot easier, less verbose, less error-prone
I disagree. I think it tends to cause very difficult to debug errors during refactoring.
Javi
https://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c11-auto-keyword
Francisco
https://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c11-auto-keyword
This question, in fact, is stating what I've just told you
Francisco
You obviously won't do auto i = 42;, but you'll surely do auto it = v.begin()
Javi
Only in particular cases. Look at the example: for ( auto x = max_size; x > 0; --x ) // unclear. could lead to the errors // since max_size is unsigned
Javi
or auto foo = bla(); // unclear. don't know which type foo has
Javi
I particularly agree with: " I suggest using it in the contexts where exact types are long, or unutterable, or not important for readability, and variables are short-lived. "
Francisco
GotW #94 Solution: AAA Style (Almost Always Auto) – Sutter’s Mill https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/
Francisco
That's an opinion by a pretty popular developer (and a commitee member) in the C++ comunity.
Javi
His opinion, which I disagree with
Francisco
Have you read the post?