English 中文(简体)
有条件的变量类型,取决于类别模板论点
原标题:Conditional variant type depending on class template arguments

我有一个班级模板,基本上想为接触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 。 而且,如果我需要增加更多的模板性论点,那么我也想避免打下一个条件,以实现我的目标。

问题回答

首先,您需要创建一种元功能,在<代码>std:variant的模板清单中附带添加另一种类型。 姓名请上condit_append:

#include <type_traits>
#include <variant>

template<bool cond, typename A, typename B>
struct condit_append;

template<bool cond, typename U, typename ...T>
struct condit_append<cond, std::variant<T...>, U>
{
    using type = std::conditional_t<cond,
        std::variant<T..., U>,
        std::variant<T...>
        >;
};

其次,要给标准海峡带来类似于标准海峡的便利:

template<bool cond, typename ...T>
using condit_append_t = typename condit_append<cond, T...>::type;

最后,将其用于你的班子:

    class ReadRequest;
    class InsertRequest;
    class DeleteRequest;
    template <bool InsertsAllowed, bool DeletesAllowed>
    class Executor {
        using Type1 = std::variant<ReadRequest>;
        using Type2 = condit_append_t<InsertsAllowed,Type1, InsertRequest>;
    public:
        using RequestType = condit_append_t<DeletesAllowed,Type2, DeleteRequest>;
         //...
    };




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签