Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove/add struct member with template

If I have the following structure:

struct data1{
   uint8_t data;
} 

template <class T>
struct packetTXMaintenance{
   uint8_t start_byte;
   uint8_t byte_count;
   uint8_t address;
   T temp_parameter;
   uint8_t command_id;
 };

For example I can instantiate the struct like this:

packetTXMaintenance<data1> value;   

But it's possible to instantiate the struct without the template temp_parameter? (In some case I want the struct without the temp_parameter member, in other case with temp_parameter member with different type T)

like image 256
Tom Avatar asked Nov 12 '13 12:11

Tom


1 Answers

You can specialize the struct for void as a template argument and in this implementation you can omit the member:

Just add this right after the declaration of your struct:

template <>
struct packetTXMaintenance <void>
{
   uint8_t start_byte;
   uint8_t byte_count;
   uint8_t address;
   uint8_t command_id;
};

So when you use packetTXMaintenance <void> variable then variable will not have the member, it's a totally different implementation.

Edit:

If you want the member to be with a different type T then you can just instantiate the struct with a different type: packetTXMaintenance<int>, in this case temp_parameter will have the type int.

like image 122
Raxvan Avatar answered Sep 22 '22 04:09

Raxvan