Simulating non-local (or free) variables in a locally defined struct - c++

This question probably only makes sense for people with knowledge on programming languages supporting closures. If you don't, please do not comment "why would you like to do this?": there are tons of legitimate reasons to do that.
It is common in functional languages to define local functions that capture the already defined local variables. In C++, that would look like (but of course is illegal):
#include <iostream>
using namespace std;
int main()
{
int x = 0;
int f() { return x + 1; }
cout << f() << endl; // would print 1
x = 2;
cout << f() << endl; // would print 3
}
To allow this, C++11 introduces lambda functions, so it is actually possible to do it in a rather nice way (though, not as nice as it generally is in functional languages ;-) ):
#include <iostream>
using namespace std;
int main()
{
int x = 0;
auto f = [&] () { return x + 1; };
cout << f() << endl; // actually compiles and prints 1
x = 2;
cout << f() << endl; // actually compiles and prints 3
}
My question is: now that it is possible to automatically capture free variables by reference for functions, wouldn't that be nice to be possible to do for locally defined structs? Ideally, I would love to be able to write:
int main()
{
int x = 0;
struct A
{
int y;
A(int y) : y(y) {}
int f() { return x + y; };
};
A a1(1);
A a2(2);
cout << a1.f() << endl; // would print 1
cout << a2.f() << endl; // would print 2
x = 2;
cout << a1.f() << endl; // would print 3
cout << a2.f() << endl; // would print 4
}
The only workaround I've found is to manually pass as argument to the constructor all the non-local (free) variables, which is a bit of a pain when there are plenty of them:
#include <iostream>
using namespace std;
int main()
{
int x = 0;
struct A
{
// meaningful members
int y;
int f() { return x + y; };
// free variables
int & x;
// Constructor
A(
// meaningful arguments
int y,
// capturing free variables
int & x
) : y(y), x(x) {}
};
A a1(1, x);
A a2(2, x);
cout << a1.f() << endl; // prints 1
cout << a2.f() << endl; // prints 2
x = 2;
cout << a1.f() << endl; // prints 3
cout << a2.f() << endl; // prints 4
}
Do you know of any other workaround that would avoid manually passing as argument all free variables, or do you know if these kind of "environment-aware" locally-defined structs are considered for future extensions of C++? (i.e., C++1y?)

What you ask for is not available, but you can get similar results by combining functions with a combination of lambdas and binders:
auto lambda = [](int i) { return x+i; };
auto a1 = std::bind(lambda,1);
auto a2 = std::bind(lambda,2);
Depending on the amount and shape of changes, you could invert the solution and have a struct that takes the lambda with the capture and then adds it's own logic.

I don't find this particularly beautiful, and I'm not entirely sure it's compliant, but neither g++ nor clang++ complains about this:
#include <iostream>
int main()
{
int x = 1;
auto l = [&](int p){
auto ll0 = [&, p]{ return p + x + 5; };
auto ll1 = [&, p]{ return p + x * 2; };
struct
{
decltype(ll0) l0;
decltype(ll1) l1;
} ret{ll0, ll1};
return ret;
};
std::cout << l(42).l0() << '\n';
auto lo = l(21);
std::cout << lo.l1() << '\n';
}
I think the creation of the unnamed struct could possibly be automated by a macro.

The lambda expressions of C++ are the capturing mechanism and inline object literals of a sort. Depending on your exact purpose, they may be more convenient than a local struct definition.
As a motivating example, consider the following:
// environment
int offset = 42;
struct local_type {
// capture environment 'by-hand'
int offset;
// purpose of the local type is to expose two overloads
int operator()(int x) const
{ return x + offset; }
double operator()(double x) const
{ return x + offset; }
} f { offset };
You can turn this on its head by doing:
int offset = 42;
auto f = make_overload([=](int x) { return offset + x; },
[=](double x) { return offset + x; });
The lambda expressions take care of capturing, the make_overload combinator takes care of building the desired object -- here, one that has an overloaded operator(). (It would be implemented best by making use of inheritance.)
This approach makes sense if you know that you'll (re)use make_overload from various places. For one-time, special uses there's no avoiding writing a specialty type, whether local or not.

Related

Affecting a function to another function in c++

So there's this code am trying to understand:
#include <iostream>
using namespace std;
int x = -2;
int h(int &x) {
x = 2 * x;
return x;
}
int g(int f) { return x; }
int &f(int &x) {
x += ::x;
return x;
}
int main() {
int x = 6;
f(::x) = h(x);
cout << f(x) << endl;
cout << g(x) << endl;
cout << h(x) << endl;
return 0;
}
Output:
24
12
48
I understood the function of the scope resolution operator (::), which is to use the global int x, but the part that i don't understand is this part: f(::x) = h(x); and this one: int g(int f){return x;}
So what am trying to understand is what happens step by step when these instructions get executed.
The line
f(::x) = h(x);
is equivalent to
operator=(f(::x), h(x));
First, one of the functions f or h are called and the returned value is passed to operator=. Then the other function is called and the returned value is passed to operator=. The order is irrelevant because in either case f changes the value of global x to -4 and returns a reference to it and h changes local x (local in main) to 12 and returns its value. Then the value 12 is assigned to global x.
The line
int g(int f) { return x; }
defines a function that returns the value of global x.
Maybe using different variable names helps understanding:
#include <iostream>
using namespace std;
int globalX = -2;
int h(int &hX) {
hX = 2 * hX;
return hX;
}
int g(int f) { return globalX; } // f is unused
int &f(int &fX) {
fX += ::globalX;
return fX;
}
int main() {
int mainX = 6;
f(::globalX) = h(mainX);
cout << f(mainX) << endl;
cout << g(mainX) << endl;
cout << h(mainX) << endl;
return 0;
}

Pass by references?

In the code below, I do not understand the difference between the two versions. In both cases, I make a passage by reference, it seems to me (I think).
Moreover, I do not understand why version 2 works, because for what I understand for the moment, the sign & allows me to give the address of a variable, so when I put function_name(int &yourInt) I technically ask the user to enter the address of an int? So I should call it like function_name(&myInt)? But here we call the function like function_name(myInt) instead.
Version 1 :
int value(int tab[], int *valeur)
{
for (int i = 0; i < 5; i++)
{
*valeur += tab[i];
}
return *valeur;
}
int main()
{
int test = 0;
int tab[10] = { 1,2,3,4,5};
std::cout << value(tab, &test) << std::endl;
std::cout << test;
}
Version 2 :
int value(int tab[], int &valeur)
{
for (int i = 0; i < 5; i++)
{
valeur += tab[i];
}
return valeur;
}
int main()
{
int test = 0;
int tab[10] = { 1,2,3,4,5};
std::cout << value(tab, test) << std::endl;
std::cout << test;
}
When the & character is part of a type declaration, it means that variable is a reference to another. So in the second function, int &valeur declares a variable named valeur that is a reference to an int. This example should help you understand:
#include <iostream>
int main() {
int a = 5;
int& b = a; // b is a reference to a
std::cout << a << '\n'; // outputs 5
std::cout << b << '\n'; // outputs 5
a = 7;
std::cout << a << '\n'; // outputs 7
std::cout << b << '\n'; // outputs 7
}
As a result, you can simply call your second function with value(tab, test) which passes the test variable by reference.
In the first example, value takes a pointer to int as second parameter. A pointer is used to save a memory address, so the function has to be called using the address operator (&), passing the address of test.
In the second example, value takes a non-const reference to int as second parameter, so you do not need to pass the memory address of test this time. What happens is almost equivalent to something like this:
int test = 0;
int& valeur = test;
So remember, the address operator:
int test;
std::cout << &test; // outputs address of "test" in memory
is not the same as a reference, which is part of the type of a variable:
int test;
int& valeur = test; // here, the type of "valeur" is "int&"

class member functions for C++ newbie [duplicate]

This question already has answers here:
Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”
(3 answers)
Closed 5 years ago.
I'm new with C++ and I'm currently studying for exams, messing around with C++ in VisualStudio and experimenting a bit. Usuall I work with Java.
I wrote a simple class to see how and if things work:
class Point
{
private:
int x;
int y;
public:
Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
};
I tried 2 simple member functions for x and y to just double the value stored in the x and y variables.
First I tried this:
void doubleX()
{
x *= 2;
};
void doubleY()
{
y *= 2;
};
Then I tried this:
void doubleX()
{
Point::x = 2 * Point::x;
};
void doubleY()
{
Point::y = 2 * Point2::y;
};
Both are put inside the class definition.
While building through VisualStudio it alwas gives me this error warning:
"Error C3867 'Point::doubleX': non-standard syntax; use '&' to create a pointer to member"
Tried to mess around with adress pointers as well but... I don't really have a clue.
I think I know how pointers basically work, but I have no idea how to use it for my case here.
Any quick solution and explanation to this problem?
Thanks in advance!
EDIT: here's my whole code, problem is in the main now
#include "stdafx.h"
#include <iostream>
using namespace std;
class Point
{
public:
int x;
int y;
Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
void doubleX()
{
x *= 2;
};
void doubleY()
{
y *= 2;
};
};
int main()
{
Point p(1,1);
int &x = p.x;
int &y = p.y;
cout << x << "|" << y;
p.doubleX; p.doubleY; //error message here
cout << x << "|" << y;
cin.get();
}
Maybe you didn't declare the member functions inside the class definition? Here is a full working example based on your class:
#include <iostream>
class Point
{
private:
int x;
int y;
public:
Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
void doubleX()
{
x *= 2; /* or this->x *= 2; */
}
void doubleY()
{
y *= 2;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p(2, 3);
std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;
p.doubleX();
p.doubleY();
std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;
return 0;
}
You can put this in a main.cpp file, compile and run it. I tested it with the g++ compiler and it works fine.
The answer given by Valy is correct. But I would like remind you that C++ offers you another choice of declaring and defining methods, that is declaring method inside the class declaration and defining them outside the class declaration. This enables you to easily separate interface and implementation into .h and .cpp files, respectively, as shown below:
Point.h
class Point
{
private:
int x;
int y;
public:
Point(int arg1, int arg2);
void doubleX();
void doubleY();
int getX();
int getY();
};
Point.cpp
#include "Point.h"
Point::Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
void Point::doubleX()
{
x *= 2;
}
void Point::doubleY()
{
y *= 2;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
// PointTest.cpp
#include "Point.h"
int main()
{
// Do something with Point here
Point pt(1, 2);
std::cout << "Original: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;
pt.doubleX();
pt.doubleY();
std::cout << "After being doubled: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;
return 0;
}
And, how to compile:
g++ -o PointTest PointTest.cpp Point.cpp
Can't comment due to reputation but it seems vc++ outputs the error message you stated if you try to call
Point::doubleX
Here's a live example of the output:
http://rextester.com/ZLCEW66682
You should create an instance of the class and call the function using parens
In your second set of functions
void doubleX()
{
Point2::x = 2 * Point2::x;
};
void doubleY()
{
Point2::y = 2 * Point2::y;
};
If you want them to be member functions of the class Point, Point::y ... this is not how you should access the member data. Only static member variables can be accessed like that. The correct way is
void doubleX()
{
this->x = 2 * this->x;
};
void doubleY()
{
this->y = 2 * this->y;
};
That is using this pointer.

what does a ampersand before double colon mean in c++?

I found this usage in the following code generated by google's protobuf.
inline void Datum::set_data(const void* value, size_t size) {
set_has_data();
//over here.
if (data_ == &::google::protobuf::internal::kEmptyString) {
data_ = new ::std::string;
}
data_->assign(reinterpret_cast<const char*>(value), size);
}
Thank you:-)!
It's two totally unrelated things, perhaps better thought of as
&(::google::protobuf::internal::kEmptyString)
& just means the address-of operator, exactly the same as if you'd done:
int xyzzy = 7;
int *pointer_to_xyzzy = &xyzzy;
The ::, on the other hand, is the global namespace specifier, to ensure you don't start looking in your current namespace.
For example, the following program:
#include <iostream>
int x = 7;
namespace xyzzy {
int x = 42;
int getxa() { return ::x; }
int getxb() { return x; }
}
int main() {
std::cout << xyzzy::getxa() << '\n';
std::cout << xyzzy::getxb() << '\n';
return 0;
}
outputs 7 followed by 42 since the getxa() function uses the global namespace specifier rather than the xyzzy one.

cyclic negative number generation in C++

I have requirement as follows.
I have to generate increment negative numbers from -1 to -100 which is used a unique id for a request. Like it should be like this: -1, -2, -3, ...-100, -1, -2, and so on. How can I do this effectively? I am not supposed to use Boost. C++ STL is fine. I prefer to write simple function like int GetNextID() and it should generate ID. Request sample program on how to do this effectively?
Thanks for your time and help
int ID = -1;
auto getnext = [=] mutable {
if (ID == -100) ID = -1;
return ID--;
};
Fairly basic stuff here, really. If you have to ask somebody on the Interwebs to write this program for you, you should really consider finding some educational material in C++.
I love the functor solution:
template <int limit> class NegativeNumber
{
public:
NegativeNumber() : current(0) {};
int operator()()
{
return -(1 + (current++ % limit));
};
private:
int current;
};
Then, you can define any generator with any limit and use it:
NegativeNumber<5> five;
NegativeNumber<2> two;
for (int x = 0; x < 20; ++x)
std::cout << "limit five: " << five() << "\tlimit two: " << two() << '\n';
You can also pass the generator as parameter to another function, with each funtor with its own state:
void f5(NegativeNumber<5> &n)
{
std::cout << "limit five: " << n() << '\n';
}
void f2(NegativeNumber<2> &n)
{
std::cout << "limit two: " << n() << '\n';
}
f5(five);
f2(two);
If you don't like the template solution to declare the limit, there's also the no-template version:
class NegativeNumberNoTemplate
{
public:
NegativeNumberNoTemplate(int limit) : m_limit(limit), current(0) {};
int operator()()
{
return -(1 + (current++ % m_limit));
};
private:
const int m_limit;
int current;
};
Using as argument to a function works in the same way, and it's internal state is transfered as well:
void f(NegativeNumberNoTemplate &n)
{
std::cout << "no template: " << n() << '\n';
}
NegativeNumberNoTemplate notemplate(3);
f(notemplate);
I hope you don't want to use it with threading, they're not thread safe ;)
Here you have all the examples; hope it helps.
Something like.... (haven't compiled)
class myClass
{
int number = 0;
int GetValue ()
{
return - (number = ((number+1) % 101))
}
}
Even a simple problem like this could lead you to several approximations, both in the algorithmic solution and in the concrete usage of the programming language.
This was my first solution using C++03. I preferred to switch the sign after computing the value.
#include <iostream>
int GetNextID() {
// This variable is private to this function. Be careful of not calling it
// from multiple threads!
static int current_value = 0;
const int MAX_CYCLE_VALUE = 100;
return - (current_value++ % MAX_CYCLE_VALUE) - 1;
}
int main()
{
const int TOTAL_GETS = 500;
for (int i = 0; i < TOTAL_GETS; ++i)
std::cout << GetNextID() << std::endl;
}
A different solution taking into account that the integer modulo in C++ takes the sign of the dividend (!) as commented in the Wikipedia
#include <iostream>
int GetNextID() {
// This variable is private to this function. Be careful of not calling it
// from multiple threads!
static int current_value = 0;
const int MAX_CYCLE_VALUE = 10;
return (current_value-- % MAX_CYCLE_VALUE) - 1;
}
int main()
{
const int TOTAL_GETS = 50;
for (int i = 0; i < TOTAL_GETS; ++i)
std::cout << GetNextID() << std::endl;
}