Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:43:37+00:00 2026-05-27T04:43:37+00:00

I have a piece of code, copied below, with really similar structs in which

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:43:37+00:00Added an answer on May 27, 2026 at 4:43 am

    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 typedefs 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()
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below piece code which runs on JDK5 private static ThreadLocal<String> messages =
I have a piece of code looking like this : TAxis *axis = 0;
I have a piece of code here that i really could use some help
I have a piece of code in ANSI C which uses the time.h library
I have a piece of code with a) which I replaced with b) purely
I have piece of code which I use to format a range of cells
I have piece of javascript code, which should process when the browser window is
I have this piece of code, which copies a plist file to the ApplicationSupport
I have a piece of code related to MapKit which works fine on my
I have following piece of code which posts some data into database: $post =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.