是否可以避免在以下代码中使用复制/移动构造函数?

时间:2018-08-24 14:26:24

标签: c++ templates c++17 variadic-templates

对于可以组成不同mixin类型的类型,请考虑以下代码。复合类型的构造函数采用可变的元组列表,这些元组表示组合类型的构造函数的参数:

#include <string>
#include <tuple>
#include <utility>

struct MixinBase {
  MixinBase() = default;
  // Note; want to delete these instead of default them.
  MixinBase(const MixinBase&) = default;
  MixinBase(MixinBase&&) = default;
};

struct MixinA : public MixinBase {
  MixinA(int, const std::string&, const std::string&) {}
};

struct MixinB : public MixinBase {
  MixinB(const std::string&, const std::string&) {}
};

template <typename... Mixins>
struct Composition : private Mixins... {
  template <typename... Packs>
  Composition(Packs&&... packs)
      : Mixins(constructMixin<Mixins>(
            std::forward<Packs>(packs),
            std::make_index_sequence<std::tuple_size_v<Packs>>{}))...
  {
  }

private:
  template <typename Mixin, typename Pack, size_t... Indexes>
  Mixin constructMixin(Pack&& arguments, std::index_sequence<Indexes...>) const
  {
    return Mixin(std::get<Indexes>(std::forward<Pack>(arguments))...);
  }
};

int main()
{
  std::string a{"a"};
  std::string b{"b"};

  Composition<MixinA, MixinB>(
      std::forward_as_tuple(7, a, b), std::forward_as_tuple(a, b));

  return 0;
}

这工作得很好,但是,我想避免通过constructMixin进行间接访问,而是直接构造每个继承的mixin对象,以便可以避免在mixin类型上使用复制/移动构造方法。这可能吗?

2 个答案:

答案 0 :(得分:4)

您可以定义一个帮助器类来支持分段构造:

template <typename T>
struct Piecewise_construct_wrapper : T {
    template <typename Tuple>
    Piecewise_construct_wrapper(Tuple&& t) : 
        Piecewise_construct_wrapper(std::forward<Tuple>(t), 
                                    std::make_index_sequence<std::tuple_size_v<Tuple>>{}) {}

    template <typename Tuple, size_t... Indexes>
    Piecewise_construct_wrapper(Tuple&& t, std::index_sequence<Indexes...>) : 
        T(std::get<Indexes>(std::forward<Tuple>(t))...) {}
};

然后,您可以让Composition继承Piecewise_construct_wrapper<Mixins>...

template <typename... Mixins>
struct Composition : private Piecewise_construct_wrapper<Mixins>... {
    template <typename... Packs>
    Composition(Packs&&... packs)
        : Piecewise_construct_wrapper<Mixins>(std::forward<Packs>(packs))...
    {
    }
};

答案 1 :(得分:3)

  

我想避免通过constructMixin进行间接操作,而是直接构造每个继承的mixin对象,以便可以避免在mixin类型上使用复制/移动构造方法。这可能吗?

您将需要Mixins选择加入以直接允许分段构建。不幸的是,这是非常重复的,因此您可以使用如下宏:

#define PIECEWISE_CONSTRUCT(Type)                               \
  template <typename Tuple>                                     \
  Type(std::piecewise_construct_t, Tuple&& tuple)               \
    : Type(std::piecewise_construct,                            \
        std::forward<Tuple>(tuple),                             \
        std::make_index_sequence<std::tuple_size_v<Tuple>>())   \
  { }                                                           \  
  template <typename Tuple, size_t... Indexes>                  \
  Type(std::piecewise_construct_t, Tuple&& tuple,               \
        std::index_sequence<Indexes...>)                        \
    : Type(std::get<Indexes>(std::forward<Tuple>(tuple))...)    \
  { }

用作:

struct MixinA : public MixinBase {
  MixinA(int, const std::string&, const std::string&) {}

  PIECEWISE_CONSTRUCT(MixinA)
};

struct MixinB : public MixinBase {
  MixinB(const std::string&, const std::string&) {}

  PIECEWISE_CONSTRUCT(MixinB)
};

template <typename... Mixins>
struct Composition : private Mixins... {
  template <typename... Packs>
  Composition(Packs&&... packs)
      : Mixins(std::piecewise_construct, std::forward<Packs>(packs))...
  { }
};

不幸的是,保证复制省略不能在构造子对象时起作用-因此,这可能是最好的选择。我认为如果没有某种嵌套包,您是否可以直接进行此操作?我很有可能只是没有足够的创造力,如果有人提出更好的建议,我会很好奇。