相关疑难解决方法(0)

在运行时选择正确的模板专业化

我有

template <int i> struct a { static void f (); };
Run Code Online (Sandbox Code Playgroud)

在代码中的不同位置完成专业化.如何仅在运行时调用正确a<i>::f的知识i

void f (int i) { a<i>::f (); } // won't compile
Run Code Online (Sandbox Code Playgroud)

我不想列出i一个大的所有可能的值switch.

编辑:

我想到了类似的东西

#include <iostream>

template <int i> struct a { static void f (); };

struct regf {
  typedef void (*F)();
  enum { arrsize = 10 };
  static F v[arrsize];
  template < int i > static int apply (F f) {
    static_assert (i < arrsize, "");
    v[i] …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

5
推荐指数
1
解决办法
1624
查看次数

标签 统计

c++ ×1

template-specialization ×1

templates ×1