#include <iostream>
void foo( int i, double d ) { std::cout << "::foo: i = " << i << " and d = " << d << '\n' ; }
struct my_vals { int x = 5; double y = 12.5; };
int main() {
constauto foo = [] ( my_vals mv ) { ::foo( mv.x, mv.y ) ; } ; // hides ::foo
foo( {} ) ; // ::foo: i = 5 and d = 12.5
foo( my_vals() ) ; // ::foo: i = 5 and d = 12.5
foo( {234} ) ; // ::foo: i = 234 and d = 12.5
foo( my_vals{234} ) ; // ::foo: i = 234 and d = 12.5
foo( { 567, 8.9 } ) ; // ::foo: i = 567 and d = 8.9
foo( my_vals{ 567, 8.9 } ) ; // ::foo: i = 567 and d = 8.9
::foo( 567, 8.9 ) ; // ::foo: i = 567 and d = 8.9 // call foo at (global) namespace scope
}
Thanks.
So why do we get errors when we use some other name, say, foot, fo, or anything else in lieu of that!?
If it's our prior function foo, why do we need to declare it this time as a new variable (a const one)?
#include <iostream>
void foo( int i, double d ) { std::cout << "::foo: i = " << i << " and d = " << d << '\n' ; }
struct my_vals { int x = 5; double y = 12.5; };
int main() {
constautofoo_wrapper = [] ( my_vals mv ) { foo( mv.x, mv.y ) ; } ;
// call foo through the wrapper
foo_wrapper( {} ) ; // ::foo: i = 5 and d = 12.5
foo_wrapper( my_vals() ) ; // ::foo: i = 5 and d = 12.5
foo_wrapper( {234} ) ; // ::foo: i = 234 and d = 12.5
foo_wrapper( my_vals{234} ) ; // ::foo: i = 234 and d = 12.5
foo_wrapper( { 567, 8.9 } ) ; // ::foo: i = 567 and d = 8.9
foo_wrapper( my_vals{ 567, 8.9 } ) ; // ::foo: i = 567 and d = 8.9
// call foo directly
foo( 567, 8.9 ) ; // ::foo: i = 567 and d = 8.9
}
Thanks. you're correct. I hadn't change the name of new foot when using to call it at the time.
Why do you call foo_wrapper an object?
And what's its type? I mean, yes, auto accepts whatever type is returned by the function (here the lambda function/expression) but what's the return type of it here, please?
#include <iostream>
usingnamespace std;
struct my_vals {
int x = 5; double y = 12.5; char ch = 'f';
void set() { x++; y++; ch++; }
};
int main() {
my_vals m;
vector<my_vals> vm;
vm.push_back(my_vals());
m.set();
vm.push_back(my_vals());
for (constauto v : vm)
cout << v.x << ' ' << v.y << ' ' << v.ch << endl;
system("pause");
return 0;
}
Both sets of values added by the vector here are the same! :(
Does vm.push_back(my_vals()); mean pushing back an object of the struct with the class's members in their initial values?
And if so, how to correctly make the new set of values (set by the set member function) added to the vector as we did above.
Yes, I know that, thank you. I meant something else.
Suppose we add a default constructed copy of the struct into the vector: vec.push_back(my_vals());
Then we modify the values in the struct using the set() function: m.set();
Now values are changed and we add this new set of values as a new copy of the struct into the vector: vec.push_back(my_vals());
Now the size of the vector is 2 and it should have two "different" copies of the struct one having the default values (before applying the set() function) and the next having the new values (after applying the set() function). But in practice, both copies are the same!
> Then we modify the values in the struct using the set() function: m.set();
> Now values are changed and we add this new set of values as a new copy of the struct into the vector:
> vec.push_back(my_vals());
vec.push_back( my_vals() ); This does *not* add (a copy) of the modified values in m. vec.push_back(m); This does.