vendredi 29 mars 2019

Conditionally define a member variable depending on the existence of the type it is made of

Suppose I have a struct for data:

struct TestData {
}

and a class with a member variable:

class TestContainer {
private:
  TestData data;
};

Both are defined in a cpp file from a macro that is used in multiple test files.

Now I want to remove the data member at compile time if there is no TestData struct defined. If a test doesn't need data then there's no need to define the data member (and it would produce a warning for not being used). I thought of using std::enable_if but failed to come up with a condition. Another approach would be to define a base template class and specializations as shown in this question, but how to specialize on the existence of a type?

How can this be done?

Aucun commentaire:

Enregistrer un commentaire