2

I wonder if we could have class like this (pseudocode):

template <class Arg1T, ... class ArgNT>
class my_class
{
public:
    my_class(Arg1T Arg1, std::string Arg1_name  ... ArgNT  ArgN, std::string ArgN_name){}
};

And if we could auto generate a function for each provided argument type on compile time using defines for example to get something like (pseudocode):

template <class Arg1T, ... class ArgNT>
class my_class
{
public:
    my_class(Arg1T Arg1, std::string Arg1_name  ... ArgNT  ArgN, std::string ArgN_name){}
    // for each Arg we want to create a function like
    ArgMT   my_class_function(std::string name)
    {
       if(name == ArgM_name)
          return ArgM;
    }

};

Is such/or a bit similar thing possible in modern C++ and how to create it?

What I try to say is: I want to generate functions, for each class type provided to constructor from some function template. And wonder how to do such thing? Here is shown how to repeat but how to repeat over provided to class arguments and thare types?

What I meant was if we'd know ammount of class templete arguments (N) we could create N variables of difrent types (one for each argument) and N strings (all private) and so we could create N functions for setters and getters of that (strings + variables) (which we would call in constructor). Main problem here is - how to solve times when you get same types twice or more, haw to get ammount of args from boost.preprocessor, how to filter same types?

Community
  • 1
  • 1
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
  • 1
    Can you please clarify what you're trying to do? What I see right now would be overloading functions based on return type. – Dark Falcon Nov 30 '11 at 18:36
  • See http://stackoverflow.com/questions/203667/c-named-parameter-idiom-vs-boostparameter-library (and Boost Parameter library) – sehe Nov 30 '11 at 21:44
  • @sehe: Parameter library seems quite unrelated... and does not provide any help with return types – myWallJSON Dec 01 '11 at 17:23

2 Answers2

0

You cannot overload functions based only on a return type, so no, it's not possible.

If you take the argument names and my_class_function out of the picture, you're left with std::tuple<T0, ..., Tn>. You can use std::get<N> to get N-th argument then (but it's entirely compile-time construct, N cannot be determined at runtime).

If you really need runtime handling, then mapping from std::string to boost::variant over N types might work.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • @myWallJSON: `std::tuple` is compile-time. You can't use `std::string` if you want compile-time construct. – Cat Plus Plus Dec 02 '11 at 10:26
  • if we'd know ammount of class templete arguments (`N`)we could create `N` variables of difrent types (one for each argument) and `N` strings (all private) and so we could create N functions for setters and getters of that string nd variables (which we would call in constructor). Main problem here is - how to solve times when you get same types twice or more. – myWallJSON Dec 02 '11 at 22:01
0

I think that in this case you could use interfaces and factory method in general.

AlexTheo
  • 4,004
  • 1
  • 21
  • 35