C++ function-like value pass - c++

first, sorry for the title but I really don´t know ho to summarize what I want to do. I am trying to write very simple "graphic" console game, just to learn basics of C++ and programming generally. When I have a function, I can pass value, or variable into that function while calling it. But I would like to do the same thing to the piece of code, but without using function. Becouse when function is called, program must actually jump to function, than return. So I thought, it would be more CPU-saving to just have that function built-in main, and just somehow select what that code should process. This could be done by passing value I want to process to some extra variable and let that "function" process that variable, but since I work with 2 dimensional fields, I need to use 2 for cycles to actually copy user-selected field to my work field. So what I want to know is, is there some way to do this more efficient? Again, please sorry my english, it´s hard to describe something in a language you don´t speak everyday.

You just described inline functions (including the function when used rather than jump and return) and references (use the caller's variables rather than copy into the function).
Inline functions just happen automatically when you turn the optimizer on, conditions permitting. Not something to worry about.
References are something you should read about in whatever book you are using to learn C++. They are declared like int foo( int &callers_var ); and can capture things like a field in a matrix.
As Roger said, never optimize until you have a functional program and can verify what is slow. That is the first rule of optimization.

Inline functions are the normal way to allow the compiler to avoid the overhead of a function call. However, it sounds like premature optimization here, and your efforts would be better spent elsewhere. A code example may help clarify what you want.

Related

Is there a way to pass an unknown number of arguments to a function?

Right now, I am trying to call a function in C++ through a Json object. The Json object would provide me with the name of the callee function and all the parameters. I will be able to extract the parameters using a for loop, but I am not sure how I can pass them in. For loop only allows me to pass arguments one by one, and I did not find a way to call a function besides passing in all the arguments at once.
I've made a temporary solution of:
if (parameter_count == 1)
func(param_1);
if (parameter_count == 2)
func(param_1, param_2);
...
This solution seems would not work for all cases since it can only work for functions with a limited number of arguments (depending on how many ifs I write). Is there a better way for this? Thanks!
EDIT: Sorry if I was being unclear. I do not know anything about func. I will be reading func from DLL based on its string name. Since I can't really change the function itself, I wouldn't be able to pass in a vector or struct directly.
Or perhaps did I have the wrong understanding? Are we allowed to pass in a single vector in place of a lot of parameters?
Sorry for making a mess through so many edits on this question. Brandon's solution with libffi works. Thanks!
So the problem as I understand it is that you have a void * pointer (which would come from your platform's DLL loading code) which "secretly" is a pointer to a function with a signature which is only known at runtime. You'd like to call this function at runtime with specified arguments.
Unfortunately, this is not possible to do cleanly with standard C++ alone. C++ cannot work with types that are not present in the program at compile-time, and since there is an infinite number of potential function signatures involved here there is no way to compile them all in.
What you'll want to do instead is manually set up the stack frame on your call stack and then jump to it, either via inline assembly or via some library or compiler extension that accomplishes this for your platform.
Here is a simple example of doing this via inline assembly. (To do this in general you will need to learn your platform's calling convention in detail, and needless to say this will constrain your program to the platform(s) you've implemented this for.)
I haven't actually tried it, but gcc has a compiler extension __builtin_apply that is apparently just meant to forward the arguments from one method wholesale to another but which could perhaps be used to accomplish something like this if you learned the (apparently opaque) description of the method.
[Update: Apparently I missed this in the comments, but Brandon mentioned libffi, a library which implements a bunch of platforms' calling conventions. This sounds like it might be the best option if you want to take this sort of approach.]
A final option would be to constrain the allowed signatures of your functions to a specified list, e.g. something like
switch(mySignature)
{
case VOID_VOID:
dynamic_cast<std::function<void(void)> *>(myPtr)();
break;
case VOID_INT:
dynamic_cast<std::function<void(int)> *>(myPtr)(my_int_arg_1);
break;
// ...
}
(Syntax of the above may not be 100% correct; I haven't tested it yet.) Whether this approach is sensible for your purposes depends on what you're doing.

forcing a function to be pure

In C++ it is possible to declare that a function is const, which means, as far as I understand, that the compiler ensures the function does not modify the object. Is there something analogous in C++ where I can require that a function is pure? If not in C++, is there a language where one can make this requirement?
If this is not possible, why is it possible to require functions to be const but not require them to be pure? What makes these requirements different?
For clarity, by pure I want there to be no side effects and no use of variables other than those passed into the function. As a result there should be no file reading or system calls etc.
Here is a clearer definition of side effects:
No modification to files on the computer that the program is run on and no modification to variables with scope outside the function. No information is used to compute the function other than variables passed into it. Running the function should return the same thing every time it is run.
NOTE: I did some more research and encountered pure script
(Thanks for jarod42's comment)
Based on a quick read of the wikipedia article I am under the impression you can require functions be pure in pure script, however I am not completely sure.
Short answer: No. There is no equivalent keyword called pure that constrains a function like const does.
However, if you have a specific global variable you'd like to remain untouched, you do have the option of static type myVar. This will require that only functions in that file will be able to use it, and nothing outside of that file. That means any function outside that file will be constrained to leave it alone.
As to "side effects", I will break each of them down so you know what options you have:
No modification to files on the computer that the program is run on.
You can't constrain a function to do this that I'm aware. C++ just doesn't offer a way to constrain a function like this. You can, however, design a function to not modify any files, if you like.
No modification to variables with scope outside the function.
Globals are the only variables you can modify outside a function's scope that I'm aware of, besides anything passed by pointer or reference as a parameter. Globals have the option of being constant or static, which will keep you from modifying them, but, beyond that, there's really nothing you can do that I'm aware.
No information is used to compute the function other than variables passed into it.
Again, you can't constrain it to do so that I'm aware. However, you can design the function to work like this if you want.
Running the function should return the same thing every time it is run.
I'm not sure I understand why you want to constrain a function like this, but no. Not that I'm aware. Again, you can design it like this if you like, though.
As to why C++ doesn't offer an option like this? I'm guessing reusability. It appears that you have a specific list of things you don't want your function to do. However, the likelihood that a lot of other C++ users as a whole will need this particular set of constraints often is very small. Maybe they need one or two at a time, but not all at once. It doesn't seem like it would be worth the trouble to add it.
The same, however, cannot be said about const. const is used all the time, especially in parameter lists. This is to keep data from getting modified if it's passed by reference, or something. Thus, the compiler needs to know what functions modify the object. It uses const in the function declaration to keep track of this. Otherwise, it would have no way of knowing. However, with using const, it's quite simple. It can just constrain the object to only use functions that guarantee that it remains constant, or uses the const keyword in the declaration if the function.
Thus, const get's a lot of reuse.
Currently, C++ does not have a mechanism to ensure that a function has "no side effects and no use of variables other than those passed into the function." You can only force yourself to write pure functions, as mentioned by Jack Bashford. The compiler can't check this for you.
There is a proposal (N3744 Proposing [[pure]]). Here you can see that GCC and Clang already support __attribute__((pure)). Maybe it will be standardized in some form in the future revisions of C++.
In C++ it is possible to declare that a function is const, which means, as far as I understand, that the compiler ensures the function does not modify the object.
Not quite. The compiler will allow the object to be modified by (potentially ill-advised) use of const_cast. So the compiler only ensures that the function does not accidentally modify the object.
What makes these requirements [constant and pure] different?
They are different because one affects correct functionality while the other does not.
Suppose C is a container and you are iterating over its contents. At some point within the loop, perhaps you need to call a function that takes C as a parameter. If that function were to clear() the container, your loop will likely crash. Sure, you could build a loop that can handle that, but the point is that there are times when a caller needs assurance that the rug will not be pulled out from under it. Hence the ability to mark things const. If you pass C as a constant reference to a function, that function is promising to not modify C. This promise provides the needed assurance (even though, as I mentioned above, the promise can be broken).
I am not aware of a case where use of a non-pure function could similarly cause a program to crash. If there is no use for something, why complicate the language with it? If you can come up with a good use-case, maybe it is something to consider for a future revision of the language.
(Knowing that a function is pure could help a compiler optimize code. As far as I know, it's been left up to each compiler to define how to flag that, as it does not affect functionality.)

Simple way to call a command-line argument as a function in C++?

Is there any way a C++ beginner can implement something like this? For example:
./timerprogram sortalgorithm.cpp
where timerprogram.cpp at some point does something like argv[1](); to run the function whose name is given by the command-line argument?
Assuming that sortalgorithm.cpp was self-contained and had an array to sort already. I don't need the timing part, just how to call as a function a command-line argument. Is there anything build-in to C++ that will allow me to do this?
No. The answer is no.
Most of the stuff you see about this are inside jokes.
There are silly ways to make it look like its working, but they are silly, and certainly not for beginners.
Function names are used mostly by the compiler, to compile the code, and figure out when something calls a function "where" it actually is. Also by the linker too, but that's beside the point.
Although some C++ implementations might provide run-time extensions or libraries that can be used to resolve an address given its symbol name, the easiest and the most portable solution is for your program to simply have an array of strings, with your function names, and a pointer to the corresponding function.
Then, your main() searches the array for the requested function name, and invokes it via its function pointer.
How to implement this simple solution is going to be your homework assignment.

"Variable" variable names c++

The Problem:
I've been searching on here for a while looking for a way to loop through variables named somewhat like variable_1, variable_2, ...., variable_n. Basically, I'm asking if there's a way to do that using a loop to achieve variable_i or, more specifically in my case, functionName_i.
What I need: I'm trying to loop an objects' array to call different functions which are sequentially-named and parallel to the objects' array ( i.e: obj[ i ]->callback_i( ) )
What I Know: Obviously, the answer here (if it were just variables) is using an array or vector. However, I need to just concatenate the functions' names sequentially somehow if it's possible.
Possible Workarounds: Everything I think of goes back to creating an array/vector of function pointers. I might get it eventually to work if I'm really out of options, but I just thought I should ask out of curiosity.
Clear Question: Is there a way to loop through sequentially-named functions using a variable int i as part of the functions' names?
Thanks!
No.
C++ does not usually store type or variable name information at runtime; if it does, it's not portable (typeid() does vary across compilers) or it's just not possible. You can't reference a variable name at runtime unless you make a system to do that sort of thing, or you use debugging information, which isn't a standard C++ feature.
This type of reflection is expensive and is more suited towards higher-level languages. C++, as a more lower-level language, strips off the sugar and just tells you "no."
You can make this type of thing in C++ if you make a naming system, but a generalized one would also require variants, a version of the NULL/Maybe idiom, attributes, checks, lots of debugging and you can do it all if you wish, but this is where you might as well switch to another language that already has the answer you're looking for and bind C++ to it.
Alternatively, use a matrix or array of functions. Then, iterate by index.

What exactly is a forward declaration with respect to memory?

I understand on a high level what a function declaration does: you declare them at the top of your file so the compiler will know which functions you are calling. However, what does the compiler exactly do with it? I know that functions are written in the text section of memory and expects arguments to be given by a register on the stack. Are function prototypes also placed within the text? Or are forward declarations simply indicators for the linkage editor to use to connect all the files together, and they are "removed" in the final product? Or does the compiler do something else with them?
I have looked around online and could not find a good resource, so if any of you can answer this or give me a resource outlining this specific phenomena, that would be greatly appreciated!
EDIT
I think there was a misunderstanding in the question, which is my mistake. My question was on how exactly does the C-compiler utilize the forward declarations. It seems from the answers below, it is used during the conversion of c-code to assembly. Is this correct?
A function prototype is simply a way to notify the compiler about how the function should be called, without having to provide any details on the implementation.
All a caller needs to know is how to call it: what parameters to pass and what to expect back.
Everything else should be hidden as much as possible so as to allow proper encapsulation, the ability for a function to change however it wants internally, without breaking anyone currently using it.
By way of example, here's a function prototype used for storing key-value pairs (both strings):
enum kvpErr kvpAdd (char *key, char *value);
This allows the compiler to ensure that you actually pass in two C strings and take an integral error code back. But it reveals nothing about the internals of the function. It may use various forms of balanced trees, a dynamically allocated array, a connection to an SQL database or a text file on an NFS server located in Outer Mongolia.
The bottom line is, you can improve the implementation in any way you wish as long as the function signature itself is kept identical.