Idiom for multi-callback structure with variable capture - c++

I have a function which takes an object and invokes various callback functions on it as its output. Example:
template<typename T>
void iterateInParallel(vector<int> const& a, vector<int> const& b, T && visitor)
{
// sometimes invokes visitor.fromA(i)
// sometimes invokes visitor.fromB(i)
// sometimes invokes visitor.fromBoth(i, j)
}
(Don't get caught up in exactly what the function does, it's an example and not relevant here.)
Now, when writing code to use a function which takes a callback object, I'll often use a local lambda with a default by-ref capture to keep my code tight and readable.
int sum = 0;
int numElems = 0;
someFunctionTakingACallback(args, [&](int x) {sum += x; numElems++; });
That's a lot nicer than the old functor approach, where I'd define a class for the purpose which took refs to sum and numElems in its constructor. And like the functor approach, it generally has zero runtime cost, since the code for the callback is known when instantiating the function.
But a lambda is but one function, so it's not workable for the iterateInParallel method above; I'm back to copying refs around.
So what I'm looking for, is a means of getting roughly the level of convenience, performance, and idiomaticity I have from lambdas' default-capture, while still being able to invoke multiple types of callbacks.
Options I've considered:
Passing multiple callback functions. This isn't too bad, and is usually what I've gone for. But it's easy to get the parameter order mixed up, and it can really bloat the argument list. It's also less self-documenting, since the name of the callbacks are nowhere in the user code.
Rewriting the inner function so that it invokes one callback, with parameters to help define what kind of callback it is. Ugly, hacky, and not the sort of thing a respectable programmer would do.
Passing a structure with a whole bunch of std::functions in it. I get the feeling that I could get this to look okay, but I also get the feeling that it would end up doing heap allocations and per-invocation indirection.
Something wacky involving the preprocessor. Heaven forfend.

Deduction guides (C++17) and designated initialization (c++20) together provide a suitable solution:
#include <iostream>
template<class F1, class F2>
struct Visitor
{
F1 fromA;
F2 fromB;
};
template<class F1, class F2>
Visitor(F1, F2) -> Visitor<F1, F2>;
template<class F1, class F2>
void f(Visitor<F1, F2> v)
{
v.fromA(1);
v.fromB("hello");
}
int main()
{
f( Visitor{
.fromA = [](int n){ std::cout << "A: " << n << "\n"; },
.fromB = [](std::string_view v){ std::cout << "B: " << v << "\n"; }
});
}
(demo)

Related

Best way of passing callback function parameters in C++

What is the best way of passing a callback function parameter in C++?
I thought of simply using templates, like this:
template <typename Function>
void DoSomething(Function callback)
This is the way used e.g. in std::sort for the comparison function object.
What about passing using &&? E.g.:
template <typename Function>
void DoSomething(Function&& callback)
What are the pros and cons of those two methods, and why does the STL uses the former e.g. in std::sort?
The
template <typename Function>
void DoSomething(Function&& callback)
… which uses a forwarding reference to pass by reference, is IMHO superior for the case where the function just uses the callback. Because a functor object, although usually small, can be arbitrarily large. Passing it by value then incurs some needless overhead.
The formal argument can end up as T&, T const& or T&& depending on the actual argument.
On the other hand, passing a simple function pointer by reference involves an extra needless indirection, which in principle could mean some slight overhead.
If in doubt whether this is significant for a given compiler, system and application, then measure.
Regarding
” why does the STL uses the former [passing by value] e.g. in std::sort?
… it's worth noting that std::sort has been there since C++98, well before forwarding references were introduced in C++11, so it could not have that signature originally.
Possibly there just has not been sufficient incentive to improve this. After all, one should generally not fix that which works. Still, extra oveloads with an “execution policy” argument are introduced in C++17.
Since this concerns a possible C++11 change, it's not covered by the usual source for rationales, namely Bjarne Stroustrup's “The design and evolution of C++”., and I do not know of any conclusive answer.
Using the template<class F> void call( F ) style, you can still get back "pass by reference". Simply do call( std::ref( your callback ) ). std::ref overrides operator() and forwards it to the contained object.
Simularly, with template<class F> void call( F&& ) style, you can write:
template<class T>
std::decay_t<T> copy( T&& t ) { return std::forward<T>(t); }
which explicitly copies something, and force call to use a local copy of f by:
call( copy(f) );
so the two styles mainly differ in how they behave by default.
Since this is C++11 (or higher): <functional> and std::function are your best friends here.
#include <iostream>
#include <functional>
#include <string>
void DoSomething(std::function<void()> callback) {
callback();
}
void PrintSomething() {
std::cout << "Hello!" << std::endl;
}
int main()
{
DoSomething(PrintSomething);
DoSomething([]() { std::cout << "Hello again!" << std::endl; });
}
In my opinion, using the && would be the superior way, basically because it avoids copy constructing the functor when the argument is an lvalue. In case of a lambda, this means avoiding to copy construct all captured values. In case of an std::function you also have an additional heap allocation unless small object optimizations are used.
But there are some cases when it could be a good thing to have a copy of the functor. One example is when using a generator function object like this:
#include <functional>
#include <cstdio>
template <typename F>
void func(F f) {
for (int i = 0; i < 5; i++) {
printf("Got %d\n", (int)f());
}
}
int main() {
auto generator0 = [v = 0] () mutable { return v++; };
auto generator1 = generator0; generator1();
printf("Printing 0 to 4:\n");
func(generator0);
printf("Printing 0 to 4 again:\n");
func(generator0);
printf("Printing 1 to 5:\n");
func(generator1);
}
If we used F&& instead, the second group of prints would print values 5 to 9 instead of 0 to 4. I guess it's up to the application/library what behaviour is preferred.
When using function pointers, we could also end up in a situation when an extra level of indirection must be used in the invoke2 function:
template <typename F>
void invoke1(F f) {
f();
}
template <typename F>
void invoke2(F&& f) {
f();
}
void fun();
void run() {
void (*ptr)() = fun;
void (&ref)() = fun;
invoke1(fun); // calls invoke1<void (*)()>(void (*)())
invoke1(&fun);// calls invoke1<void (*)()>(void (*)())
invoke1(ptr); // calls invoke1<void (*)()>(void (*)())
invoke1(ref); // calls invoke1<void (*)()>(void (*)())
invoke2(fun); // calls invoke2<void (&)()>(void (&)())
invoke2(&fun);// calls invoke2<void (*)()>(void (*&&)())
invoke2(ptr); // calls invoke2<void (*&)()>(void (*&)())
invoke2(ref); // calls invoke2<void (&)()>(void (&)())
}
The statement invoke2(&fun); puts the address to the function on the stack, then sends the reference (i.e. an address) to this temporary stack slot to the invoke2 function. The invoke2 function must then use an extra memory lookup to read the reference on the stack before it can use it. The extra indirection applies for invoke2(ptr) too. In all other cases, the address to the function is sent directly to the invoke1/invoke2 function and does not need any extra indirection.
This example shows one interesting difference between a function reference and a function pointer.
Of course, compiler optimisation such as inlining could easily get rid of this extra indirection.

modern c++ alternative to function pointers

I've been using function pointers till now, like this format in c++. I do have some uses now and then and I'm wondering is there anything else introduced in c++11/14 as their alternative.
#include <iostream>
using namespace std;
void sayHello();
void someFunction(void f());
int main() {
someFunction(sayHello);
return 0;
}
void sayHello(){
std::cout<<"\n Hello World";
}
void someFunction(void f()){
f();
}
I did take a look at this question but couldn't understand any advantages over traditional use of function pointers. Also I would like to ask , is there anything wrong (not recommended) thing with using function pointers since I never see anyone using them. Or any other alternative present.
The question you mention suggest std::function but does not emphasize (or mentions at all) its value when combined with std::bind.
Your example is the simplest possible, but suppose you have a
std::function<void (int, int)> f ;
A function pointer can do more or less the same things. But suppose that you need a function g(int) which is f with second parameter bound to 0. With function pointers you can't do much, with std::function you can do this:
std::function<void(int)> g = std::bind(f, _1, 0) ;
As an alternative to traditional function pointers, C++11 introduced template alias which combined with variadic templates could simplify the function pointer sintax. below, an example of how to create a "template" function pointer:
template <typename R, typename ...ARGS> using function = R(*)(ARGS...);
It can be used this way:
void foo() { ... }
int bar(int) { ... }
double baz(double, float) { ... }
int main()
{
function<void> f1 = foo;
function<int, int> f2 = bar;
function<double, double, float> f3 = baz;
f1(); f2({}); f3({}, {});
return 0;
}
Also, it can deal neatly with function overloads:
void overloaded(int) { std::cout << "int version\n"; }
void overloaded(double) { std::cout << "double version\n"; }
int main()
{
function<void, int> f4 = overloaded;
function<void, double> f5 = overloaded;
f4({}); // int version
f5({}); // double version
return 0;
}
And can be used as a pretty neat way to declare function-pointers parameters:
void callCallback(function<int, int> callback, int value)
{
std::cout << "Calling\n";
std::cout << "v: " << callback(value) << '\n';
std::cout << "Called\n";
}
int main()
{
function<int, int> f2 = bar;
callCallback(f2, {});
return 0;
}
This template alias could be used as an alternative of std::function which doesn't have its drawbacks nor its advantages (good explanation here).
Live demo
As a brief, I think that template alias combined with variadic templates is a good, nice, neat and modern C++ alternative to raw function pointers (this alias still are function pointers after all) but std::function is good, nice, neat and modern C++ as well with good advantages to take into account. To stick in function pointers (or alias) or to choose std::function is up to your implementation needs.
Also I would like to ask , is there anything wrong (not recommended)
thing with using function pointers since I never see anyone using
them.
Yes. Function pointers are terrible, awful things. Firstly, they do not support being generic- so you cannot take a function pointer that, say, takes std::vector<T> for any T. Secondly, they do not support having bound state, so if at any time in the future, anybody, ever, wishes to refer to other state, they are completely screwed. This is especially bad since this includes this for member functions.
There are two approaches to taking functions in C++11. The first is to use a template. The second is to use std::function.
The template kinda looks like this:
template<typename T> void func(F f) {
f();
}
The main advantages here are that it accepts any kind of function object, including function pointer, lambda, functor, bind-result, whatever, and F can have any number of function call overloads with any signature, including templates, and it may have any size with any bound state. So it's super-duper flexible. It's also maximally efficient as the compiler can inline the operator and pass the state directly in the object.
int main() {
int x = 5;
func([=] { std::cout << x; });
}
The main downside here is the usual downsides of templates- it doesn't work for virtual functions and has to be defined in the header.
The other approach is std::function. std::function has many of the same advantages- it can be any size, bind to any state, and be anything callable, but trades a couple off. Mainly, the signature is fixed at type definition time, so you can't have a std::function<void(std::vector<T>)> for some yet-to-be-known T, and there may also be some dynamic indirection/allocation involved (if you can't SBO). The advantage of this is that since std::function is a real concrete type, you can pass it around as with any other object, so it can be used as a virtual function parameter and such things.
Basically, function pointers are just incredibly limited and can't really do anything interesting, and make the API incredibly unflexible. Their abominable syntax is a piss in the ocean and reducing it with a template alias is hilarious but pointless.
I did take a look at this question but couldn't understand any
advantages over traditional use of function pointers. Also I would
like to ask , is there anything wrong (not recommended) thing with
using function pointers since I never see anyone using them.
Normal "global" functions typically don't/can't have state. While it's not necessarily good to have state during traversal in functional programming paradigm, sometimes state might come in handy when it relates orthogonally to what has been changed (heuristics as example). Here functors (or function objects) have the advantage.
Normal functions don't compose very well (creating higher level functions of lower level functions.
Normal functions don't allow for binding additional parameters on the fly.
Sometimes normal functions can act as replacement for lambdas, and visa versa, depending on the context. Often one wouldn't want to write a special function just because you have some very local/specific requirement during "container traversal".

c++11 lambda expressions

Today I was doing some catch-up on c++11 (as we have not moved on yet). One of the reasons to switch as stated by a lot of people seems to be lambda expressions. I am still unsure on how they offer something new.
For instance using c++11:
#include <iostream>
int main()
{
auto func = [] () { std::cout << "Hello world" << std::endl; };
func();
}
seems to be very similar to:
#include <iostream>
#define FUNC( )\
do { std::cout << "Hello world" << std::endl; } while(0)
int main()
{
FUNC();
}
What would lambda expressions offer me that I can't currently already do?
http://msdn.microsoft.com/en-us/library/vstudio/dd293608.aspx sums up the main points and more on the subject in great detail. Here is the salient excerpt:
A lambda combines the benefits of function pointers and function
objects and avoids their disadvantages. Like a function objects, a
lambda is flexible and can maintain state, but unlike a function
object, its compact syntax doesn't require a class definition. By
using lambdas, you can write code that's less cumbersome and less
prone to errors than the code for an equivalent function object.
There are examples on the site showing more differences and comparisons.
Also...conventional wisdom is never use macros in C++:
http://scienceblogs.com/goodmath/2007/12/17/macros-why-theyre-evil/
A less obvious benefit of lambdas when used with the standard algorithms is that the programmer is not required to think of a name for the lambda function, and naming things is considered to be a difficult task in programming.
Additionally, the code executed via the standard algorithms is often used to invoke a member function(s) on each object in the supplied range, with the name of the functor, or function, often just parroting the name of the member function being invoked and adding nothing to readability of the code. Contrived example:
struct object
{
void execute() const {}
};
void run_execute(object const& o) { o.execute(); }
std::vector<object> v;
std::for_each(v.begin(), v.end(), run_execute);
std::for_each(v.begin(), v.end(), [](object const& o) { o.execute(); });
Admittedly this is a minor benefit but still pleasant.

Is there any use case for class inside function after introduction of lambda?

From the wikipedia article about Lambda functions and expressions:
users will often wish to define predicate functions near the place
where they make the algorithm function call. The language has only one
mechanism for this: the ability to define a class inside of a
function. ... classes defined in functions do not permit them to be used in templates
Does this mean that use of nested structure inside function is silently deprecated after C++0x lambda are in place ?
Additionally, what is the meaning of last line in above paragraph ? I know that nested classes cannot be template; but that line doesn't mean that.
I'm not sure I understand your confusion, but I'll just state all the facts and let you sort it out. :)
In C++03, this was legal:
#include <iostream>
int main()
{
struct func
{
void operator()(int x) const
{
std::cout << x << std::endl;
}
};
func f; // okay
f(-1); // okay
for (std::size_t i = 0; i < 10; ++i)
f(i) ; // okay
}
But if we tried doing this, it wasn't:
template <typename Func>
void exec(Func f)
{
f(1337);
}
int main()
{
// ...
exec(func); // not okay, local classes not usable as template argument
}
That left us with an issue: we want to define predicates to use for this function, but we can't put it in the function. So we had to move it to whatever outer scope there was and use it there. Not only did that clutters that scope with stuff nobody else needed to know about, but it moved the predicate away from where it's used, making it tougher to read the code.
It could still be useful, for the occasional reused chunk of code within the function (for example, in the loop above; you could have the function predicate to some complex thing with its argument), but most of the time we wanted to use them in templates.
C++0x changes the rules to allow the above code to work. They additionally added lambdas: syntax for creating function objects as expressions, like so:
int main()
{
// same function as above, more succinct
auto func = [](int x){ std::cout << x << std::endl; };
// ...
}
This is exactly like above, but simpler. So do we still have any use for "real" local classes? Sure. Lambda's fall short of full functionality, after all:
#include <iostream>
template <typename Func>
void exec(Func func)
{
func(1337);
}
int main()
{
struct func
{
// note: not possible in C++0x lambdas
void operator()(const char* str) const
{
std::cout << str << std::endl;
}
void operator()(int val) const
{
std::cout << val << std::endl;
}
};
func f; // okay
f("a string, ints next"); // okay
for (std::size_t i = 0; i < 10; ++i)
f(i) ; // okay
exec(f); // okay
}
That said, with lambda's you probably won't see local classes any more than before, but for completely different reasons: one is nearly useless, the other is nearly superseded.
Is there any use case for class inside function after introduction of lambda ?
Definitely. Having a class inside a function is about:
localising it as a private implementation detail of the code intending to use it,
preventing other code using and becoming dependent on it,
being independent of the outer namespace.
Obviously there's a threshold where having a large class inside a function harms readability and obfuscates the flow of the function itself - for most developers and situations, that threshold is very low. With a large class, even though only one function is intended to use it, it may be cleaner to put both into a separate source file. But, it's all just tuning to taste.
You can think of this as the inverse of having private functions in a class: in that situation, the outer API is the class's public interface, with the function kept private. In this situation, the function is using a class as a private implementation detail, and the latter is also kept private. C++ is a multi-paradigm language, and appropriately gives such flexibility in modelling the hierarchy of program organisation and API exposure.
Examples:
a function deals with some external data (think file, network, shared memory...) and wishes to use a class to represent the binary data layout during I/O; it may decide to make that class local if it only has a few fields and is of no use to other functions
a function wants to group a few items and allocate an array of them in support of the internal calculations it does to derive its return value; it may create a simple struct to wrap them up.
a class is given a nasty bitwise enum, or perhaps wants to reinterpret a float or double for access to the mantisa/exponent/sign, and decides internally to model the value using a struct with suitable-width bitfields for convenience (note: implementation defined behaviours)
classes defined in functions do not permit them to be used in templates
I think you commented that someone else's answer had explained this, but anyway...
void f()
{
struct X { };
std::vector<X> xs; // NOPE, X is local
}
Defining structures inside functions was never a particularly good way to deal with the lack of predicates. It works if you have a virtual base, but it's still a pretty ugly way to deal with things. It might look a bit like this:
struct virtual_base {
virtual void operator()() = 0;
};
void foo() {
struct impl : public virtual_base {
void operator()() { /* ... */ }
};
register_callback(new impl);
}
You can still continue to use these classes-inside-functions if you want of course - they're not deprecated or crippled; they were simply restricted from the very start. For example, this code is illegal in versions of C++ prior to C++0x:
void foo() {
struct x { /* ... */ };
std::vector<x> y; // illegal; x is a class defined in a function
boost::function<void()> z = x(); // illegal; x is used to instantiate a templated constructor of boost::function
}
This kind of usage was actually made legal in C++0x, so if anything the usefulness of inner classes has actually be expanded. It's still not really a nice way of doing things most of the time though.
Boost.Variant.
Lambdas don't work with variants, as variants need objects that have more than one operator() (or that have a templated operator()). C++0x allows local classes to be used in templates now, so boost::apply_variant can take them.
As Tony mentioned, a class inside a function is not only about predicates. Besides other use cases, it allows to create a factory function that creates objects confirming to an interface without exposing the implementing class. See this example:
#include <iostream>
/* I think i found this "trick" in [Alexandrescu, Modern C++ Design] */
class MyInterface {
public:
virtual void doSomethingUseful() = 0;
};
MyInterface* factory() {
class HiddenImplementation : public MyInterface {
void doSomethingUseful () {
std::cout << "Hello, World!" << std::endl;
}
};
return new HiddenImplementation();
}
int main () {
auto someInstance = factory();
someInstance->doSomethingUseful();
}

Why override operator()?

In the Boost Signals library, they are overloading the () operator.
Is this a convention in C++? For callbacks, etc.?
I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me.
Any insight as to the reason for this overload?
One of the primary goal when overloading operator() is to create a functor. A functor acts just like a function, but it has the advantages that it is stateful, meaning it can keep data reflecting its state between calls.
Here is a simple functor example :
struct Accumulator
{
int counter = 0;
int operator()(int i) { return counter += i; }
}
...
Accumulator acc;
cout << acc(10) << endl; //prints "10"
cout << acc(20) << endl; //prints "30"
Functors are heavily used with generic programming. Many STL algorithms are written in a very general way, so that you can plug-in your own function/functor into the algorithm. For example, the algorithm std::for_each allows you to apply an operation on each element of a range. It could be implemented something like that :
template <typename InputIterator, typename Functor>
void for_each(InputIterator first, InputIterator last, Functor f)
{
while (first != last) f(*first++);
}
You see that this algorithm is very generic since it is parametrized by a function. By using the operator(), this function lets you use either a functor or a function pointer. Here's an example showing both possibilities :
void print(int i) { std::cout << i << std::endl; }
...
std::vector<int> vec;
// Fill vec
// Using a functor
Accumulator acc;
std::for_each(vec.begin(), vec.end(), acc);
// acc.counter contains the sum of all elements of the vector
// Using a function pointer
std::for_each(vec.begin(), vec.end(), print); // prints all elements
Concerning your question about operator() overloading, well yes it is possible. You can perfectly write a functor that has several parentheses operator, as long as you respect the basic rules of method overloading (e.g. overloading only on the return type is not possible).
It allows a class to act like a function. I have used it in a logging class where the call should be a function but i wanted the extra benefit of the class.
so something like this:
logger.log("Log this message");
turns into this:
logger("Log this message");
Many have answered that it makes a functor, without telling one big reason why a functor is better than a plain old function.
The answer is that a functor can have state. Consider a summing function - it needs to keep a running total.
class Sum
{
public:
Sum() : m_total(0)
{
}
void operator()(int value)
{
m_total += value;
}
int m_total;
};
You may also look over the C++ faq's Matrix example. There are good uses for doing it but it of course depends on what you are trying to accomplish.
The use of operator() to form functors in C++ is related to functional programming paradigms that usually make use of a similar concept: closures.
A functor is not a function, so you cannot overload it.
Your co-worker is correct though that the overloading of operator() is used to create "functors" - objects that can be called like functions. In combination with templates expecting "function-like" arguments this can be quite powerful because the distinction between an object and a function becomes blurred.
As other posters have said: functors have an advantage over plain functions in that they can have state. This state can be used over a single iteration (for example to calculate the sum of all elements in a container) or over multiple iterations (for example to find all elements in multiple containers satisfying particular criteria).
Start using std::for_each, std::find_if, etc. more often in your code and you'll see why it's handy to have the ability to overload the () operator. It also allows functors and tasks to have a clear calling method that won't conflict with the names of other methods in the derived classes.
Functors are basically like function pointers. They are generally intended to be copyable (like function pointers) and invoked in the same way as function pointers. The main benefit is that when you have an algorithm that works with a templated functor, the function call to operator() can be inlined. However, function pointers are still valid functors.
One strength I can see, however this can be discussed, is that the signature of operator() looks and behaves the same across different types. If we had a class Reporter which had a member method report(..), and then another class Writer, which had a member method write(..), we would have to write adapters if we would like to use both classes as perhaps a template component of some other system. All it would care about is to pass on strings or what have you. Without the use of operator() overloading or writing special type adapters, you couldn't do stuff like
T t;
t.write("Hello world");
because T has a requirement that there is a member function called write which accepts anything implicitly castable to const char* (or rather const char[]). The Reporter class in this example doesn't have that, so having T (a template parameter) being Reporter would fail to compile.
However, as far I can see this would work with different types
T t;
t("Hello world");
though, it still explicitly requires that the type T has such an operator defined, so we still have a requirement on T. Personally, I don't think it's too wierd with functors as they are commonly used but I would rather see other mechanisms for this behavior. In languages like C# you could just pass in a delegate. I am not too familiar with member function pointers in C++ but I could imagine you could achieve the same behaviour there aswell.
Other than syntatic sugar behaviour I don't really see the strengths of operator overloading to perform such tasks.
I am sure there are more knowingly people who have better reasons than I have but I thought I'd lay out my opinion for the rest of you to share.
Another co-worker pointed out that it could be a way to disguise functor objects as functions. For example, this:
my_functor();
Is really:
my_functor.operator()();
So does that mean this:
my_functor(int n, float f){ ... };
Can be used to overload this as well?
my_functor.operator()(int n, float f){ ... };
Other posts have done a good job describing how operator() works and why it can be useful.
I've recently been using some code that makes very extensive use of operator(). A disadvantage of overloading this operator is that some IDEs become less effective tools as a result. In Visual Studio, you can usually right-click on a method call to go to the method definition and/or declaration. Unfortunately, VS isn't smart enough to index operator() calls. Especially in complex code with overridden operator() definitions all over the place, it can be very difficult to figure out what piece of code is executing where. In several cases, I found I had to run the code and trace through it to find what was actually running.
Overloading operator() can make the class object calling convention easier. Functor is one of the applications of operator() overloading.
It is easy to get confused between Functor and user-defined conversion function.
Below 2 examples show the difference between
1. Functor
2. User-defined conversion function
1. Functor:
struct A {
int t = 0;
int operator()(int i) { return t += i; } // must have return type or void
};
int main() {
A a;
cout << a(3); // 3
cout << a(4); // 7 (Not 4 bcos it maintaines state!!!)
}
2. User-defined conversion function:
struct A {
int t = 3;
operator int() { return t; } // user-defined conversion function
// Return type is NOT needed (incl. void)
};
int main() {
cout << A(); // 3 - converts the object{i:3} into integer 3
A a;
cout << a; // 3 - converts the object{i:3} into integer 3
}