如何分组不同的静态类?

问题描述 投票:1回答:1

我有一个带有不同特化的模板化静态类,如下所示:

template<typename Parameter >
class MyClass
{};

 template<>
class MyClass<Parameter1>
{
public:
     static constexpr Integer myarray[]={0};
     static constexpr Integer myarraysize=1;    
 };

  template<>
class MyClass<Parameter2>
{
public:
     static constexpr Integer myarray[]={0,1};
     static constexpr Integer myarraysize=2;    
 };

现在我想以某种方式将这些信息分组到一个新类中

template<typename MyClass1, typename MyClasses... >
class MyGroupClass{
//do something...}

我可以作为可变参数模板参数给出不同的类,然后我可以访问不同的静态方法。

例如,我想以MyGroupClass[n]::myarraysize的方式访问与第n个myarraysize相关的MyClass

我想我可以创建一个元组(所以有std::get<n>()),但我不清楚如何做到这一点,因为我没有这样的单一静态类的构造函数。毕竟,这些类是静态的。

有可能实现我想要的吗?如果是的话,请你赐教?谢谢。

c++ c++11 static variadic-templates stdtuple
1个回答
0
投票

我想以MyGroupClass [n] :: myarraysize来访问与第n个MyClass相关的myarraysize。我想我可以创建一个元组(所以有std :: get()),

在我看来,你必须区分两种情况。

(1)当不同类别的myarraysize具有不同类型时,您可以创建不同大小的std::tuple,并使用std::get()来提取值。

以身作则

template <typename ... Ts>
struct MyGroupStruct
 {
   const std::tuple<decltype(Ts::myarraysize)...> tpl { Ts::myarraysize... };

   template <std::size_t N>
   auto get () const -> decltype(std::get<N>(tpl))
    { return std::get<N>(tpl); }
 };

从C ++ 14开始,您可以避免get()的尾随返回类型并简单地编写

   template <std::size_t N>
   auto get () const
    { return std::get<N>(tpl); }

观察到MyGroupStruct::get()接收索引(N)作为模板参数。所以它需要一个编译时已知的值。这是必要的,因为从模板方法返回的类型因索引而异,因此必须是已知的编译时间。

(2)当不同类别的所有myarraysize属于同一类型时,您也可以创建该类型的std::array;某事

template <typename ... Ts>
struct MyGroupStruct
 {
   using myType = typename std::tuple_element<0u, 
      std::tuple<decltype(Ts::myarraysize)...>>::type;

   const std::array<myType, sizeof...(Ts)> arr {{ Ts::myarraysize... }};

   myType & get (std::size_t n)
    { return arr[n]; }
 };

请注意,在这种情况下,get()的返回值始终相同,因此它可以接收运行时索引作为(非模板)参数。

以下是不同类型(基于元组的)案例的完整编译示例

#include <tuple>
#include <string>
#include <iostream>

struct Par1 {};
struct Par2 {};
struct Par3 {};
struct Par4 {};

template <typename>
struct MyStruct;

template <>
struct MyStruct<Par1>
 { static constexpr int myarraysize {1}; };

constexpr int MyStruct<Par1>::myarraysize;

template <>
struct MyStruct<Par2>
 { static constexpr long myarraysize {2l}; };

constexpr long MyStruct<Par2>::myarraysize;

template <>
struct MyStruct<Par3>
 { static constexpr long long myarraysize {3ll}; };

constexpr long long MyStruct<Par3>::myarraysize;

template <>
struct MyStruct<Par4>
 { static const std::string myarraysize; };

const std::string MyStruct<Par4>::myarraysize {"four"};

template <typename ... Ts>
struct MyGroupStruct
 {
   const std::tuple<decltype(Ts::myarraysize)...> tpl { Ts::myarraysize... };

   template <std::size_t N>
   auto get () const -> decltype(std::get<N>(tpl))
    { return std::get<N>(tpl); }
 };

int main ()
 {
   MyGroupStruct<MyStruct<Par1>, MyStruct<Par2>,
                 MyStruct<Par3>, MyStruct<Par4>> mgs;

   std::cout << mgs.get<0>() << std::endl;
   std::cout << mgs.get<1>() << std::endl;
   std::cout << mgs.get<2>() << std::endl;
   std::cout << mgs.get<3>() << std::endl;

   static_assert( std::is_same<int const &,
                               decltype(mgs.get<0>())>::value, "!" );
   static_assert( std::is_same<long const &,
                               decltype(mgs.get<1>())>::value, "!" );
   static_assert( std::is_same<long long const &,
                               decltype(mgs.get<2>())>::value, "!" );
   static_assert( std::is_same<std::string const &,
                               decltype(mgs.get<3>())>::value, "!" );
 }

现在是等于基于类型(基于数组)的案例的完整编译示例

#include <tuple>
#include <array>
#include <string>
#include <iostream>

struct Par1 {};
struct Par2 {};
struct Par3 {};
struct Par4 {};

template <typename>
struct MyStruct;

template <>
struct MyStruct<Par1>
 { static constexpr int myarraysize {1}; };

constexpr int MyStruct<Par1>::myarraysize;

template <>
struct MyStruct<Par2>
 { static constexpr int myarraysize {2}; };

constexpr int MyStruct<Par2>::myarraysize;

template <>
struct MyStruct<Par3>
 { static constexpr int myarraysize {3}; };

constexpr int MyStruct<Par3>::myarraysize;

template <>
struct MyStruct<Par4>
 { static const int myarraysize {4}; };

const int MyStruct<Par4>::myarraysize;

template <typename ... Ts>
struct MyGroupStruct
 {
   using myType = typename std::tuple_element<0u, 
         std::tuple<decltype(Ts::myarraysize)...>>::type;

   const std::array<myType, sizeof...(Ts)> arr {{ Ts::myarraysize... }};

   myType & get (std::size_t n)
    { return arr[n]; }
 };

int main ()
 {
   MyGroupStruct<MyStruct<Par1>, MyStruct<Par2>,
                 MyStruct<Par3>, MyStruct<Par4>> mgs;

   std::cout << mgs.get(0) << std::endl;
   std::cout << mgs.get(1) << std::endl;
   std::cout << mgs.get(2) << std::endl;
   std::cout << mgs.get(3) << std::endl;

   static_assert( std::is_same<int const &,
                               decltype(mgs.get(0))>::value, "!" );
 }

- 编辑 -

OP问道

如果我想访问myarray而不仅仅是myarraysize,如何更改代码?

使用C风格的数组有点复杂,因为您无法使用C风格的数组初始化C样式数组的元组。

我建议你使用C样式数组的引用元组。

所以,给定一些MyClasses只有myarray(为什么在你推断它时可以添加大小?)

template <typename>
struct MyStruct;

template <>
struct MyStruct<Par1>
 { static constexpr int myarray[] {0}; };

constexpr int MyStruct<Par1>::myarray[];

// other MyStruct specializations ...

你可以添加一个参考元组(元组,而不是std::array,因为int[1]int[2]int[3]int[4]都是不同的类型)和std::array<std::size_t, sizeof...(Ts)>的大小。

我的意思是......你可以写下如下内容

template <typename ... Ts>
struct MyGroupStruct
 {
   std::tuple<decltype(Ts::myarray) & ...> const tpl { Ts::myarray... };

   std::array<std::size_t, sizeof...(Ts)> const arr
    {{ sizeof(Ts::myarray)/sizeof(Ts::myarray[0])... }};

   template <std::size_t N>
   auto getArr () const -> decltype(std::get<N>(tpl))
    { return std::get<N>(tpl); }

   std::size_t getSize (std::size_t n) const
    { return arr[n]; }
 };

这是什么意思“const - > decltype(std :: get(tpl))”?

constdecltype()无关。

const,在方法参数列表之后,表示该方法也可以由常量对象使用,因为它不会更改成员变量。

关于decltype()寻找更多信息的“尾随返回类型”。

简而言之,对于C ++ 11,我的想法就是

auto foo () -> decltype(something)
 { return something; }

auto说“照顾 - >返回类型”和decltype(something)是“something的类型”

你也可以写

decltype(something) foo ()
 { return something; }

如果在函数参数列表之前知道something,但是当auto包含模板参数时,-> decltype(something) / something形式变得有用

以身作则

template <typename T1, typename T2>
auto sum (T1 const & t1, T2 const & t2) -> decltype(t1+t2)
 { return t1+t2; }

从C ++ 14开始,“尾随返回类型”的使用较少,因为您可以简单地编写

template <typename T1, typename T2>
auto sum (T1 const & t1, T2 const & t2)
 { return t1+t2; }

因为auto对编译器说“从return表达式推导出返回类型”(所以在这种情况下来自t1+t2)。

这避免了很多冗余。

我们可以在C ++ 11中使用“auto”吗?

auto作为回归类型?没有尾随返回类型?

不幸的是只能从C ++ 14开始。

跟随myarray和推断尺寸的另一个完整示例。

#include <tuple>
#include <array>
#include <string>
#include <iostream>

struct Par1 {};
struct Par2 {};
struct Par3 {};
struct Par4 {};

template <typename>
struct MyStruct;

template <>
struct MyStruct<Par1>
 { static constexpr int myarray[] {0}; };

constexpr int MyStruct<Par1>::myarray[];

template <>
struct MyStruct<Par2>
 { static constexpr int myarray[] {0, 1}; };

constexpr int MyStruct<Par2>::myarray[];

template <>
struct MyStruct<Par3>
 { static constexpr int myarray[] {0, 1, 2}; };

constexpr int MyStruct<Par3>::myarray[];

template <>
struct MyStruct<Par4>
 { static constexpr int myarray[] {0, 1, 2, 3}; };

constexpr int MyStruct<Par4>::myarray[];

template <typename ... Ts>
struct MyGroupStruct
 {
   std::tuple<decltype(Ts::myarray) & ...> const tpl { Ts::myarray... };

   std::array<std::size_t, sizeof...(Ts)> const arr
    {{ sizeof(Ts::myarray)/sizeof(Ts::myarray[0])... }};

   template <std::size_t N>
   auto getArr () const -> decltype(std::get<N>(tpl))
    { return std::get<N>(tpl); }

   std::size_t getSize (std::size_t n) const
    { return arr[n]; }
 };

int main ()
 {
   MyGroupStruct<MyStruct<Par1>, MyStruct<Par2>,
                 MyStruct<Par3>, MyStruct<Par4>> mgs;

   std::cout << mgs.getSize(0) << std::endl;
   std::cout << mgs.getSize(1) << std::endl;
   std::cout << mgs.getSize(2) << std::endl;
   std::cout << mgs.getSize(3) << std::endl;

   static_assert( std::is_same<std::size_t,
                               decltype(mgs.getSize(0))>::value, "!" );

   std::cout << mgs.getArr<0>()[0] << std::endl;
   std::cout << mgs.getArr<1>()[1] << std::endl;
   std::cout << mgs.getArr<2>()[2] << std::endl;
   std::cout << mgs.getArr<3>()[3] << std::endl;

   static_assert( std::is_same<int const (&)[1],
                               decltype(mgs.getArr<0>())>::value, "!" );
   static_assert( std::is_same<int const (&)[2],
                               decltype(mgs.getArr<1>())>::value, "!" );
   static_assert( std::is_same<int const (&)[3],
                               decltype(mgs.getArr<2>())>::value, "!" );
   static_assert( std::is_same<int const (&)[4],
                               decltype(mgs.getArr<3>())>::value, "!" );    
 }
© www.soinside.com 2019 - 2024. All rights reserved.