【问题标题】:Is it possible to "bundle" template parameters in C++?是否可以在 C++ 中“捆绑”模板参数?
【发布时间】:2017-05-25 04:08:05
【问题描述】:

有没有办法将模板参数“捆绑”在一起以避免重复?

我有几个类和函数都使用相同的三个模板参数。拥有一个使用每个类/函数一次的函数并不罕见。生成的代码很快就会变得非常混乱。 有没有更简洁的方式来编写这段代码?

// ContextFactory is a pointer to functions that instantiate objects that are subtypes of MetricContext
template<typename VertexID, typename EdgeMembershipType, typename SetBitmap>
using ContextFactory = MetricContext <VertexID, EdgeMembershipType, SetBitmap> *(*)(const char *);

template<typename VertexID, typename EdgeMembershipType, typename SetBitmap>
    static vector<ContextFactory<VertexID, EdgeMembershipType, SetBitmap>> buildCFList() {
      vector<ContextFactory<VertexID, EdgeMembershipType, SetBitmap>> answer;
      answer.push_back(MetricContext<VertexID, EdgeMembershipType, SetBitmap>::template make<NeoContext<VertexID, EdgeMembershipType, SetBitmap >>);
      return answer;
    };

请注意,这个函数几乎有一半是字符串&lt;VertexID, EdgeMembershipType, SetBitmap&gt;&gt;的重复,但是这个字符串的每次使用都适用于不同的类或函数,所以我认为别名不会起作用。

(如果有帮助,此函数的目的是创建一个指向函数的指针数组,这些函数将创建 MetricContext&lt;VertexID, EdgeMembershipType, SetBitmap&gt;&gt; 的子类型的对象

【问题讨论】:

  • 使用 C++11,您可以将它们捆绑到模板参数包中。但是,您会失去一些可读性。
  • 首先,您可以使用auto作为返回值。
  • FWIW:当我用auto (static auto buildCFList()) 替换返回类型时,我收到此错误'auto' return without trailing return type; deduced return types are a C++14 extension
  • 您需要在静态函数之外使用别名“ContextFactory”吗?如果没有,你可以把它拉进去,去掉一行模板参数。
  • 我在静态函数之外唯一需要它的地方是返回类型。

标签: c++ c++11 templates using-declaration


【解决方案1】:

是的,这是可能的。让我们定义一个小助手类来保存类型列表:

template <class... > struct pack { };

还有一个使用pack 中的内容实例化模板的元函数:

template <template <class... > class T, class P>
struct unpack_;

template <template <class... > class T, class... P>
struct unpack_<T, pack<P...>> {
    using type = T<P...>;
};

template <template <class... > class T, class P>
using unpack = typename unpack_<T, P>::type;

我们现在可以存储和使用我们的参数包:

template <class A, class B, class C>
struct Foo { };

using Params = pack<int, float, double>;

unpack<Foo, Params> f; // f is a Foo<int, float, double>

See it live on Coliru

【讨论】:

  • 这种方法的缺点是 id 无助于减少 buildCFList 实现的冗长,因为在函数内部,我们使用包的事实丢失了。
  • @Jean-BernardJansen 我看的越多,我越相信我完全错误地阅读了这个问题......
【解决方案2】:

比@Quentin 更具体的方法是让您的模板依赖于单个参数 - 预计该参数具有 VertexID、EdgeMembershipType 和 SetBitmap 的类型定义。

// ContextFactory is a pointer to functions that instantiate objects that are 
// subtypes of MetricContext
template<typename Types>
using ContextFactory = MetricContext <Types> *(*)(const char *);

template<typename Types>
    static vector<ContextFactory<Types>> buildCFList() {
      vector<ContextFactory<Types>> answer;
      answer.push_back(MetricContext<Types>::template make<NeoContext<Types>>);
      return answer;
    };

请注意,当您想真正使用其中一种类型定义时,您需要使用例如:typename Types::VertexID。

(理想情况下,您会为模板参数想出一个比 Types 更好的名称。)

【讨论】:

    【解决方案3】:

    如果您使用的是 C++11,则可以使用 std::tuple 将变量组合为一个。

    一个更简单的例子来理解同样的可以是

    template <typename A, typename B, typename C>
    void fn() {
    
     typedef std::tuple<A,B,C> myTuple;
    
      myTuple tpl;
      cout<<sizeof(std::get<0>(tpl))<<endl;;
      cout<<sizeof(std::get<1>(tpl))<<endl;
      cout<<sizeof(std::get<2>(tpl))<<endl;
    
    }
    
    int main() {
    
     fn<int,char,long>();
    
     return 0;
    }
    

    对于特定于您的问题的问题,您可以将元组向量创建为

    template <typename A, typename B, typename C>
    void fn() {
    
        using mycomb = std::tuple<A,B,C>;
    
        vector<mycomb> v1;
        v1.push_back(make_tuple(10,'c',20.0));
    }
    

    这样,你就不需要重复了。元组 getter 函数起初有点尴尬。上面的cout 示例演示了如何访问元组参数

    希望对你有帮助

    【讨论】:

    • 可悲的是,我发现 tuple 非常适合用作通用捆绑包。它做的太多了,让事情构建得太慢了。
    猜你喜欢
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    相关资源
    最近更新 更多