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

问题描述 投票:0回答:3

我有很多使用相同模板参数列表的 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;

我意识到我可以创建一个单独的类来定义类型并使用它。但我想知道是否有一种方法可以在不定义新类的情况下做到这一点。

编辑

我不想使用宏。

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

c++ c++11 templates using
3个回答
4
投票

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

但需要一点工作。

您可以使用具有双层模板管理的结构体

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
层,具有可变模板元素(在您的示例中,
A
B
C

您还可以添加

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>>{}, "!" );
 }

1
投票

您可以用定义替换别名,虽然不完美,但很简单的解决方案,它可以工作。

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

注意:定义末尾没有

;


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


template <template<typename, typename Index, typename, typename, Index, Index, Index, Index> typename T>
using TypeInject = T<float, std::size_t, bool, std::string, 1, 2, 3, 4>;

使用方法如下:

TypeInject <A> a;
TypeInject <B> b;
TypeInject <C> c;
© www.soinside.com 2019 - 2024. All rights reserved.