可以根据c ++中的模板args更改类成员吗

Xu *_*Hui 4 c++ c++98

我希望可以根据模板参数更改类成员。我想要类似的东西

template<int value>
class MyClass
{
public:
   void print()
   {
      // using the member
      std::cout << sizeData() << std::endl;
      for (int i=0;i<sizeData();i++)
      {
         std:cout << data[i] << std::endl;
      }
   }
   static int sizeData()
   {
     #if value == 1
        return 3;
     #endif
     #if value == 2
        return 6;
     #endif
   }
   static int sizeArray()
   {
     #if value == 1
        return 40;
     #endif
     #if value == 2
        return 200;
     #endif
   }
private:
   #if value == 1
      int data[3];
      const static int array[40];
   #endif
   #if value == 2
      int data[6];
      const static int array[200];
   #endif
}
Run Code Online (Sandbox Code Playgroud)

不知道能不能用c++实现。

谢谢你的时间。

添加

许多先生已经在 C++11 和 C++17 中给出了答案。谢谢你的所有建议。

如果代码能用C++98解决就完美了。因为我的代码应该在只支持 C++98 的平台上运行。

Nat*_*ica 5

您可以使用std::conditional来选择您想要的成员

template<int value>
class MyClass
{
public:
   static int sizeData()
   {
       return (value == 1) ? 3 : 6; // needs to be improved
   }
   static int sizeArray()
   {
       return sizeof(array) / sizeof(array[0]);  // size of array divided by size of element is then number of elements in the array
   }
private:

   std::conditional_t<value == 1, int[3], int[6]> data;
   const static std::conditional_t<value == 1, int[40], int[200]> array;

};
Run Code Online (Sandbox Code Playgroud)

虽然std::conditional不是 C++98 的一部分,但它仅使用 C++98 C++ 实现,因此您可以使用参考站点链接中的可能实现来实现自己。你可以看到与

#include <iostream>

template<bool B, class T, class F>
struct conditional { typedef T type; };
 
template<class T, class F>
struct conditional<false, T, F> { typedef F type; };

template<int value>
class MyClass
{
public:
    static int sizeData()
    {
       return (value == 1) ? 3 : 6; // needs to be improved
    }
    static int sizeArray()
    {
       return sizeof(array) / sizeof(array[0]);
    }
private:

    typename conditional<value == 1, int[3], int[6]>::type data;
    const static typename conditional<value == 1, int[40], int[200]>::type array;

};

int main()
{
    MyClass<1> m;
    std::cout << m.sizeData();
}
Run Code Online (Sandbox Code Playgroud)

在这个活生生的例子中


这种分崩离析的sizeData原因data是不是static功能是。为了避免代码重复,std::conditional我们可以使用 enum 技巧来获取数组大小的编译时间常量,而不是使用

#include <iostream>

template<int value>
class MyClass
{
public:
    static int sizeData()
    {
       return data_size; // now improved
    }
    static int sizeArray()
    {
       return array_size;
    }
private:
    enum { data_size = (value == 1) ? 3 : 6 }; // this is required to be a compile time constant
    enum { array_size = (value == 1) ? 40 : 200 }; // these need to become before the array members
    int data[data_size]; // less verbose
    const static int array[array_size];
};

int main()
{
    MyClass<1> m;
    std::cout << m.sizeData();
}
    
    
Run Code Online (Sandbox Code Playgroud)

  • @XuHui 几乎任何解决方案都会很冗长。这是“旧”C++ 的主要缺点之一。C++11 和更新版本使这变得更容易,因为它们多年来一直了解我们实际如何使用以及想要使用该语言。 (2认同)