问题
| ■ Probelm definition____________________
I'm trying to design a super-flexible, but memory efficient module satisfying below properties.
- It can switch OFF unnecessary member variables depending on the situation.
- Which variables it will own are determined at the compile-time.
I somehow made one which determines its member list "before" the compile-time, using macros and enumerator flags. see below :
▼ TraitSwitch.h
#pragma once
// Macros to switch-off source codes themselves.
#define ON 1
#define OFF 0
#define TRI_AREA_INFO ON
#define TRI_CENTROID_INFO ON
#define TRI_NORMAL_INFO OFF // When the normal vector info is unnecessary.
...
▼ TriangleTraits.h
#pragma once
#include <cstdint>
#include "TraitSwitch.h"
enum TriangleTrait : uint8_t
{
NONE = 0, // 0000 0000
#if (TRI_AREA_INFO == ON)
AREA = 1, // 0000 0001
#endif
#if (TRI_CENTROID_INFO == ON)
CENTROID = 2, // 0000 0010
#endif
#if (TRI_NORMAL_INFO == ON) // | Inactive Preprocessor Block
NORMAL_VECTOR = 4, // 0000 0100 |
#endif
... // more traits
ALL = 255 // 1111 1111
}
// Need some additional overloaded bitwise-operators (&, |, |=, etc ...)
▼ Triangle.h
#pragma once
#include "TriangleTraits.h"
class Triangle
{
public:
Triangle() {}
~Triangle() {}
#if (TRI_AREA_INFO == ON)
double area;
#endif
#if (TRI_CENTROID_INFO == ON)
double centroid[3]; // x, y, z
#endif
#if (TRI_NORMAL_INFO == ON) // | Inactive Preprocessor Block
double normal[3]; // x, y, z |
#endif
...
TriangleTrait alreadyComputed; // To avoid redundant works.
void ComputeTraits(TriangleTrait _requested)
{
if (((_requested & TriangleTrait::AREA) != 0)
&& ((alreadyComputed & _requested) == 0))
{
this->ComputeArea();
alreadyComputed |= TriangleTrait::AREA;
}
... // do the same things for centroid, normal
}
private:
void ComputeArea();
void ComputeCentroid();
void ComputeNormal();
...
}
then, C++ IntelliSense on the object may show : this
▼ main.cpp
#include <iostream>
#include "Triangle.h"
int main(void)
{
Triangle tri;
tri.ComputeTraits(TriangleTrait::AREA | TriangleTrait::CENTROID);
std::cout << "area : " << tri.area << "m²" << std::endl;
std::cout << "centroid : ("
<< tri.centroid[0] << ","
<< tri.centroid[1] << ","
<< tri.centroid[2] << ")" << std::endl;
...
}
Firstly Triangle.h looks quite ugly, and even if it looks good, this method determines class members "before" the compile-time, anyway.
| ■ Question summary____________________
"How to design a template class with switchable members, which are determined at the compile-time."
Here's the very thing what I want :
▼ main.cpp
...
int main(void)
{
Triangle<__MACRO_DEFINED_TRAIT_SWITCH(AREA)> tri1; // This owns area info only
tri1.area;
Triangle<__MACRO_DEFINED_TRAIT_SWITCH(AREA | CENTROID)> tri2; // This owns area & centroid info
tri2.area;
tri2.centroid;
Triangle<__MACRO_DEFINED_TRAIT_SWITCH(AREA | NORMAL)> tri3; // This owns area & normal vector info
tri3.area;
tri3.normal;
...
Triangle<__MACRO_DEFINED_TRAIT_SWITCH(AREA | CENTROID | NORMAL)> tri4; // This owns area & centroid & normal vector info
tri4.area;
tri4.centroid;
tri4.normal;
...
}
I guess using templates combined with macros (with Tag-dispatching method, maybe?) will do exactly what I want, but have no any clear idea.
回答1:
After a few days of distress, I found a way. So I'll answer my question myself.
The soultion was tooooo simple. Just use multiple inheritances with variadic templates. See below:
▼TriangleTraits.h
struct AREA
{
double area;
};
struct CENTROID
{
double centroid[3];
};
struct NORMAL
{
double normal[3];
};
... // more traits
// Multiple Inheritances with variadic template
template<class... Traits>
struct TriangleWithTraits : Traits...
{
};
▼main.cpp
#include "TriangleTraits.h" // Just include this one is enough
int main()
{
TriangleWithTraits<AREA> tri1;
tri1.area;
TriangleWithTraits<AREA, CENTROID> tri2;
tri2.area;
tri2.centroid;
TriangleWithTraits<AREA, NORMAL> tri3;
tri3.area;
tri3.normal;
TriangleWithTraits<AREA, CENTROID, NORMAL> tri4;
tri4.area;
tri4.normal;
tri4.centroid;
...
}
回答2:
Cannot use preprocessor to parse template parameter, preprocesssor does not know of C++.
But there's std::conditional
and there's Empty Base optimization.
So you can have bases with data class parts, and std::conditional
to select between them and empty base:
struct EmptyBase {};
struct BaseWithChar
{
char a;
};
struct BaseWithLongLong
{
long long b;
long long c;
};
template<int Flags>
struct A
: std::conditional<(Flags & 1), BaseWithChar, EmptyBase>::type
, std::conditional<(Flags & 2), BaseWithLongLong, EmptyBase>::type
{
};
int main() {
std::cout << sizeof(A<1>) << std::endl;
std::cout << sizeof(A<2>) << std::endl;
std::cout << sizeof(A<3>) << std::endl;
return 0;
}
And with later C++ standards, there's std::coditional_t
and [[no_unique_address]]
:
using namespace std;
struct EmptyMember {};
template<int Flags>
struct A
{
[[no_unique_address]] typename std::conditional_t<(Flags & 1), char, EmptyMember> a;
[[no_unique_address]] typename std::conditional_t<(Flags & 2), long long, EmptyMember> b;
[[no_unique_address]] typename std::conditional_t<(Flags & 2), long long, EmptyMember> c;
};
int main() {
std::cout << sizeof(A<1>) << std::endl;
std::cout << sizeof(A<2>) << std::endl;
std::cout << sizeof(A<3>) << std::endl;
return 0;
}
But [[no_unique_address]]
requires C++20, and without [[no_unique_address]]
empty member will use some space. Looks like it does not work with Visual C++, even with 2019 preview. I've posted feebdack.
With earlier C++, when you don't have std::conditional
or boost::conditional
, you can easily implement your own, it is just a template with true and false specialization. See "possible implementation" on cppreference page
来源:https://stackoverflow.com/questions/60733043/how-to-design-a-template-class-with-switchable-member-variables-at-compile-time