我有一个班级模板,基本上想为接触APIC用户的变式类型设定一个模板:
template <bool InsertsAllowed, DeletesAllowed>
class Executor {
...
public:
// Ideally, would be
// std::variant<ReadRequest, {InsertRequest only if InsertsAllowed}, {DeleteRequest only if DeletesAllowed}>
// e.g. InsertsAllowed = true, DeletesAllowed = False ==> std::variant<ReadRequest, InsertRequest>
// InsertsAllowed = true, DeletesAllowed = True ==> std::variant<ReadRequest, InsertRequest, DeleteRequest>
using RequestType = ...
makeRequest(const RequestType &r);
...
}
迄今为止,我可以提出的最佳解决办法是:
template <bool InsertsAllowed, DeletesAllowed>
class Executor {
...
public:
struct BAD_TYPE = {};
using RequestType = std::variant<
ReadRequest,
std::conditional<InsertsAllowed, InsertRequest, BAD_TYPE>,
std::conditional<DeleletesAllowed, DeleteRequest, BAD_TYPE>>;
makeRequest(const RequestType &r);
...
}
但是,这确实是我为实现的,也是最清洁的解决办法。
我需要能够提及<代码>RequestType,因此,不幸的是,我可以取代BAD_TYPE
。 而且,如果我需要增加更多的模板性论点,那么我也想避免打下一个条件,以实现我的目标。