模板参数列表的 C++ 别名

标签 c++ c++11 templates using

我有很多使用相同模板参数列表的 C++ 类

template<typename T, typename Index, typename Bool, typename Data, Index n_x, Index n_u, Index n_c, Index n_w>
class A {
   ...
};

template<typename T, typename Index, typename Bool, typename Data, Index n_x, Index n_u, Index n_c, Index n_w>
class B {
   ...
};

template<typename T, typename Index, typename Bool, typename Data, Index n_x, Index n_u, Index n_c, Index n_w>
class C {
   ...
};

你明白了。然后我将它们实例化为

A<T, Index, Bool, Data, n_x, n_u, n_c, n_w> a;
B<T, Index, Bool, Data, n_x, n_u, n_c, n_w> b;
C<T, Index, Bool, Data, n_x, n_u, n_c, n_w> c;

有没有办法以某种方式为这组模板参数创建一个别名,这样我就不必重新输入参数列表了?

我有这样的想法......

using Params = T, Index, Bool, Data, n_x, n_u, n_c, n_w;
A<Params> a;
B<Params> b;
C<Params> c;

我意识到我可以创建一个单独的类来定义类型,然后使用它。但是我想知道是否有一种方法可以在不定义新类的情况下执行此操作。

编辑

我不想使用宏。

我也不想使用默认值,因为这需要确保默认值在一堆文件中是统一的。我意识到我可以定义一个新的默认 header 并将其包含在所有文件中,但这似乎是糟糕的编程。

最佳答案

不完全是你问的,但也不是那么不同......

但需要一些工作。

你可以用结构foo来解决,有双层模板管理。

template <typename T, typename Index, typename Bool, typename Data,
          Index I1, Index I2, Index I3, Index I4>
struct foo
 { 
   template <template <typename, typename X, typename, typename, X, X, X, X>
                       class Cont>
   using type = Cont<T, Index, Bool, Data, I1, I2, I3, I4>;
 };

第一层,结构层,具有您要修复的类型/值(T、Index、Bool、Data、n_x、n_u、n_c、n_w,在您的示例中)。

第二层,using 层,带有可变模板元素(ABC ,在您的示例中。

你也可以添加一个using别名foot_t来简化使用

template <template <typename, typename X, typename, typename, X, X, X, X>
                    class Cont, typename C>
using foo_t = typename C::template type<Cont>;

现在您可以使用 using

修复类型和值(第一层)
using f = foo<T, Index, Bool, Data, n_x, n_u, n_c, n_w>;

并使用foo_t声明激活第二层的变量

foo_t<A, f> a;
foo_t<B, f> b;
foo_t<C, f> c;

下面是一个完整的工作示例

#include <vector>
#include <iostream>

template <typename T, typename Index, typename Bool, typename Data,
          Index n_x, Index n_u, Index n_c, Index n_w>
class A { };

template <typename T, typename Index, typename Bool, typename Data,
          Index n_x, Index n_u, Index n_c, Index n_w>
class B { };

template <typename T, typename Index, typename Bool, typename Data,
          Index n_x, Index n_u, Index n_c, Index n_w>
class C { };

template <typename T, typename Index, typename Bool, typename Data,
          Index I1, Index I2, Index I3, Index I4>
struct foo
 { 
   template <template <typename, typename X, typename, typename, X, X, X, X>
                       class Cont>
   using type = Cont<T, Index, Bool, Data, I1, I2, I3, I4>;
 };

template <template <typename, typename X, typename, typename, X, X, X, X>
                    class Cont, typename C>
using foo_t = typename C::template type<Cont>;


int main ()
 {
   using T     = float;
   using Index = std::size_t;
   using Bool  = bool;
   using Data  = std::vector<std::string>;

   constexpr std::size_t n_x { 0U };
   constexpr std::size_t n_u { 1U };
   constexpr std::size_t n_c { 2U };
   constexpr std::size_t n_w { 3U };

   using f = foo<T, Index, Bool, Data, n_x, n_u, n_c, n_w>;

   foo_t<A, f> a;
   foo_t<B, f> b;
   foo_t<C, f> c;

   static_assert( std::is_same<decltype(a),
                     A<T, Index, Bool, Data, n_x, n_u, n_c, n_w>>{}, "!" );
   static_assert( std::is_same<decltype(b),
                     B<T, Index, Bool, Data, n_x, n_u, n_c, n_w>>{}, "!" );
   static_assert( std::is_same<decltype(c),
                     C<T, Index, Bool, Data, n_x, n_u, n_c, n_w>>{}, "!" );
 }

关于模板参数列表的 C++ 别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276611/

相关文章:

c++ - T 的模板特化 -> std::vector<T>

c++ - 空值包扩展是否匹配类型包或可选类型参数?

c++ - 如果我使用 malloc() 而不是堆栈数组,OpenGL 不会渲染对象?

c++ - Allegro 5 循环遍历具有相同名称的配置文件值

用于性能优化的 C++ 分离函数

c++ - 稍后在构造函数中重新启动异常指令 delete this

c++ - 如果存在专门化,如何限制模板化函数?

jQuery 模板从 Twitter 中提取 JSON 提要

c# - 将托管 unsigned char* 转换为 jbyte*

c++ - 消除重载方法歧义失败