c++ - How to declare an operator-like expression as a function call? -


goal:

describe way provide function calls in such way given function "t f(t a, t b)" expression "e" written in code "aeb" calls function "f(a, b)"!

motivation:

to provide abbreviated forms of function calls, when syntax increase clarity of code versus standard form.

example:

#include <iostream> #include <random> #include <valarray>  using namespace std;  constexpr unsigned int sides = 20u;  random_device rndngen; uniform_int_distribution<unsigned int> dice[sides];  // function called via in-order unsigned int operatord(unsigned int number, unsigned int sides) {   valarray<unsigned int> dice(number);    for(unsigned int & : dice)   {     = dice[sides](rndngen);   }    return dice.sum(); }  int main() {   // desired call syntax   int x = 1 + 1d6;   // executes operator=(x, operator+(1, operatord(1, 6)))   cout << x << endl; // displays number in [2, 7] } 

note chosen expression d cannot collide expression "1.d", dot required , d may not followed number.

whether or not function's name , symbolising expression (in example "operatord" , "d") need identical of minor interest; bonus if both cases possible.

failed solution attempts:

1) operator definition

operator definition not functionality of c++ explained here.

2) macro definition

the #define directive not support explicit parameter definitions:

#define (x)d(y) d(x,y) // error: macro names must identifiers #define xdy d(x,y) // not recognise x , y variables 

possible solutions not want use:

1) operator overloads function object described here

triples number of true function calls every apparent call.

related problems:

access one-dimensional array m matrix syntax 'x = m[row][column]'. necessary define 2 overloads of operator[] 2 different classes of object: matrix , vector (either row or column). 1 'm[row][column]' call creates anonymous vector m[row], on [column] executed. ... every single 'm[row]' call generates object constructor, on own member function operator[] called once, (being anonymous) discarded.

use matrix operations in quasi-mathematic notation:

class matrix {   matrix invert();   matrix transpose(); };  matrix m; // m = m.invert(); m = m^-1;    // assuming here whole "^-1" call sign. m = m sup-1; // assuming here whole "sup-1" call sign. // m = m.transpose(); m = m^t;    // dito here; 't' not supposed separate token. m = m supt; // should work in spite of operator^, not defined custom matrix class. 

hide encapsulation of objects:

class value {   member x; }; class encapsulatedvalue<value t> {   t value;    encapsulatedvalue(t value) : value{value}   {}    member x()   {     member y = t.x;     /* modifies y */     return y;   } };  value temp; encapsulatedvalue v(temp); member z; // z = v.x(); z = v.x; 

you can't overload operators on arguments of built-in type (or raw pointers).

apart you're pretty free whatever want.

what question?


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -