简体   繁体   中英

How to simplify structs using a template?

I have a piece of code, copied below, with really similar structs in which just one member changes type. I am looking to simplify it. I was considering using a template, but I am not too sure how would it be the syntax between a struct and a template. Any pointer would be greatly appreciated.


typedef struct pos_parameter{
    float magnitud;
    bool new_value;
} pos_parameter;

typedef struct feed_parameter{
    double magnitud;
    bool new_value;
} feed_parameter;

typedef struct speed_parameter{
    long magnitud;
    bool new_value;
} speed_parameter;

typedef struct g_code_parameter{
    int magnitud;
    bool new_value;
} g_code_parameter;

typedef struct position{
    pos_parameter X;
    pos_parameter Y;
    pos_parameter Z;
    pos_parameter A;
} position;

typedef struct move{
    position pos;
    feed_parameter feedrate;
    speed_parameter speed;
    g_code_parameter g_code;
} move;
template <typename T>
struct parameter{
    T magnitud;
    bool new_value;
};

Example use:

parameter<float> pos_parameter;
parameter<double> feed_parameter;
parameter<long> speed_parameter;
parameter<int> code_parameter;

Hope that helps.

I guess the first four could be :

template<typename T>
struct pos_parameter{
    T magnitud;
    bool new_value;
};

One quick observation before we get in to genericizing the magnitud member variable. Your use of the typedef struct {/*...*/} name; syntax is not needed, and very non-idiomatic in C++. In C this may be needed, but C++ is not C. In C++, you can simply:

struct Gizmo
{
  /* ... */
};

Now, in order to genericize the type of the magnitud member variable, you can create a class template like this:

template<class MAG> struct basic_parameter
{
    MAG magnitud;
    bool new_value;
};

You would declare an instance of such a thing like this:

basic_parameter<int> myparam;

You can also use a typedef to create a kind of "alias" for certian specializations of basic_parameter , like this:

typedef basic_param<int> int_param;  // Now int_param is shorthand for basic_param<int>
/* ... */
int_param myparam; // myparam is of type basic_param<int>

You can use this technique to create typedef s for all the different kinds of parameters you were using in the position and move structures, and make it so that you don't have to change the code for those types.

Here is a complete solution:

template<class MAG> struct basic_parameter
{
    MAG magnitud;
    bool new_value;
};

typedef basic_parameter<float> pos_parameter;
typedef basic_parameter<double> feed_parameter;
typedef basic_parameter<long> speed_parameter;
typedef basic_parameter<int> g_code_parameter;

struct position {
    pos_parameter X;
    pos_parameter Y;
    pos_parameter Z;
    pos_parameter A;
};

struct move
{
    position pos;
    feed_parameter feedrate;
    speed_parameter speed;
    g_code_parameter g_code;
};

int main()
{
}
template<typename T>
struct generic_parameter
{
    T magnitud;
    bool new_value;
};

There you go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM