Call a function if the template contains a member variable C++

58 Views Asked by At

I am stuck trying to find out a way to achieve this. A derived class calls the write function and if there is a existence of the variable timestamp in the template parameter, then it is incremented otherwise the data directly written. I am trying to find a way to perform function selection based on existence of timestamp member in the template struct.

Here is the relevant code.

template<class InputType>
class Writer
{

void write(const InputType & data)
{
    if(data.timestamp) exists ,
       then data.timestamp++;
}

}

Sample InputType

struct InputType
{
uint8_t timestamp;
bool flag;
uint8_t other_stuff;
}

I am not sure how to use type traits here to acheive that.

I have been able to figure out how to detect if the member is present but not how to call different member functions based on that result

0

There are 0 best solutions below