Personal fields or members that give function/method properties?

I'm looking for a set of personal-to-[methods/functions] data fields. A library or something that I can include and activate to give these fields would be nice.
What is a "personal-to-[methods/functions] data field"?
sorry,... variables that give like a functions argument list and all similar data to methods and functions
Can you show us an example of what you mean?
Just a guess on my part......

You want a variable that can hold different type multiple values? Maybe std::tuple would be what you are looking for.

http://www.cplusplus.com/reference/tuple/tuple/tuple/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <tuple>
#include <string>
#include <iomanip>

int main()
{
   std::tuple<int, char> t1;

   std::get<0>(t1) = 5;
   std::get<1>(t1) = 'A';

   std::cout << std::get<0>(t1) << ' ' << std::get<1>(t1) << '\n';

   std::tuple<int, double, std::string> t2 { std::make_tuple(100, 3.14159, "A string") };

   // structured binding, requires C++17
   auto [ i, d, s ] { t2 };

   std::cout << i << ' ' << d << ' ' << std::quoted(s) << '\n';
}

5 A
100 3.14159 "A string"
Variables that give like a functions argument list and all similar data to methods and functions


Okay - a way to separate a list of arguments from something Callable:
https://en.cppreference.com/w/cpp/named_req/Callable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <tuple>
#include <iostream>
#include <functional>

int sum(int a, int b) { return a + b; }
struct foo
{ 
  int product(int m) const { return n_ * m; }
  int n_ = 2;
};

int main()
{
  { 
    auto args = std::make_tuple(2, 3);
    std::cout << std::apply(sum, args) << '\n';
  }
 
  {
    foo f;
    auto args = std::make_tuple(std::cref(f), 3);
    std::cout << std::apply(&foo::product, args) << '\n';
    
    // alternatively
    std::cout << std::invoke(&foo::product, std::cref(f), 3) << '\n';
  }
}

http://coliru.stacked-crooked.com/a/80f35a8017b3ab94

Unfortunately, building/manipulating std::tuple often requires a little bit of meta-programming know-how: the indices trick, etc.

Last edited on
I believe the OP is refering to either std::variant or some sort of custom implementation of setters and geters. aka. fields. (very small classes for that purpose)

malibor wrote:
I believe the OP is refering to either std::variant....

Something I hadn't considered since it is C++17, and I am still nibbling at what that standard offers. std::variant is on my "to learn" list.
Topic archived. No new replies allowed.