Implementing a function which returns function definition as string

Mahesh Bansod

I'm making a program in which a function, say double f(double), is defined. I want to create a function(or a macro) codeToStr(f) which will return a std::string/const char* containing definition of the function f.
I believe that by using preprocessing directive something can be done but I can't figure out how.
Is there a way to do this besides using file IO and reading from the source file?

==
Reason for the question:
My university gave me the assignment to implement the "Trapezoidal rule" for numerical integration and they've asked us to hard-code the function f(x). After I submit the code, they will modify that function from the source code to test various cases. I'd like my output to have a way of displaying the function definition of the function that is going to be integrated. This is the reason why I want to implement codeToStr.

Jean-Baptiste Yunès

You can try something like that:

#include <iostream>
#include <string>

#define real_stringify(s) #s
#define stringify(...) real_stringify(__VA_ARGS__)

#define MYFUNC double f(double d) { \
        return d*2; \
        }

MYFUNC

int main() {
        std::cout << f(5.5) << std::endl;
        std::string s = stringify(MYFUNC);
        std::cout << s << std::endl;
        return 0;
}

But this necessitate that the function is defined under the scope of the preprocessor. You can add more tricks to hide it as separating definition/declaration, etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript definition for a function which returns based on nested keys in parameter

pinvoke to function which returns string array

Junit test function which returns a string

Unable to compile function which returns parts of a string

Function which returns a TABLE after splitting a string

Function which returns function scheme

Is there a function to print a definition as a string

String not recognized in function definition

Definition of function which is the class member

Why does this definition returns a function?

Type definition for function in a namespace that returns this

SQL (Postgres) function definition - RETURNS

TypeScript: function that returns a generic Type which extends Record<string, string>

function which returns true is the string is in upper-case using XSLT

A function that returns a string which resemples a playing board in python

User defined function returns value error which should be string

Create a function which takes in a float as input and returns a string containing a number

Testing a function which returns an object that returns a function which return a boolean

Polymorphic function in class which is implementing abstract class

Implement a function which returns a pointer

A Function Which Returns Parent Functions

Function which returns multiple values

How to test function which returns function with parameters?

Function returns boolean not string

The function returns empty string

Function or macro definition which one to use

Implementing a C function that splits a string on a given character and returns an array of strings after the split (along with length of array)

write a function which accepts a string and returns a new string with only the capital letters passed to the string

How to dry up type definition of function that returns another function