模板/泛型类中的非常量静态成员-c++;vs c#

模板/泛型类中的非常量静态成员-c++;vs c#,c#,c++,templates,generics,C#,C++,Templates,Generics,在c#中,静态成员对于每个泛型类都是唯一的,如本例所示 using System; //A generic class public class GenTest<T> { //A static variable - will be created for each type on refraction static CountedInstances OnePerType = new CountedInstances(); //a data member priva

在c#中,静态成员对于每个泛型类都是唯一的,如本例所示

using System;

//A generic class
public class GenTest<T>
{
  //A static variable - will be created for each type on refraction
  static CountedInstances OnePerType = new CountedInstances();

  //a data member
  private T mT;

  //simple constructor
  public GenTest(T pT)
  {
    mT = pT;
  }
}

//a class
public class CountedInstances
{
  //Static variable - this will be incremented once per instance
  public static int Counter;

  //simple constructor
  public CountedInstances()
  {
    //increase counter by one during object instantiation
    CountedInstances.Counter++;
    Console.WriteLine(Counter);
  }
}

public class staticTest {
  static void Main(string[] args) {
    //main code entry point
    //at the end of execution, CountedInstances{{Not a typo|.}}Counter = 2
    GenTest<int> g1 = new GenTest<int>(1);
    GenTest<int> g11 = new GenTest<int>(11);
    GenTest<int> g111 = new GenTest<int>(111);
    GenTest<double> g2 = new GenTest<double>(1.0);
  }
}
使用系统;
//泛型类
公共阶层绅士
{
//将为折射上的每种类型创建一个静态变量
静态CountedInstances OnePerType=新CountedInstances();
//数据成员
私人T-mT;
//简单构造函数
公共绅士(T pT)
{
mT=pT;
}
}
//班级
公共阶级立场
{
//静态变量-每个实例将增加一次
公共静态计数器;
//简单构造函数
公众立场
{
//在对象实例化期间将计数器增加1
CountedInstances.Counter++;
控制台。写线(计数器);
}
}
公共类静态测试{
静态void Main(字符串[]参数){
//主代码入口点
//在执行结束时,CountedInstances{{非打字错误|.}}计数器=2
GenTest g1=新的GenTest(1);
GenTest g11=新GenTest(11);
GenTest g111=新GenTest(111);
GenTest g2=新GenTest(1.0);
}
}

什么关于C++?我已经尝试过检查自己,但是翻译成C++似乎忽略了静态成员。
#include <iostream>
using namespace std;

class CountedInstances {
public:
  static int Counter;
  CountedInstances() {
    Counter++;
    cout << Counter << endl;
  }
};

int CountedInstances::Counter = 0;

template<class T> class GenTest {
  static CountedInstances OnePerType;
  T mT;
public:
  GenTest(T pT) {
    mT = pT;
  }
};

template<class T> CountedInstances GenTest<T>::OnePerType = CountedInstances();

int main() {
  GenTest<int> g1(1);
  GenTest<int> g11(11);
  GenTest<int> g111(111);
  GenTest<double> g2(1.0);
  cout << CountedInstances::Counter << endl;
  //CountedInstances c1;
  //CountedInstances c2;
}
#包括
使用名称空间std;
阶级立场{
公众:
静态整数计数器;
计数状态(){
计数器++;
来自(我的)的cout:

如果编译器隐式实例化包含静态成员的类模板,则这些静态成员不会隐式实例化。仅当编译器需要静态成员的定义时,编译器才会实例化静态成员

因此,要强制编译器有效地实例化
GenTest::OnePerType
GenTest::OnePerType
,您必须以某种方式引用它们。例如,您可以在构造函数中添加
(void)&OnePerType;
(实时示例:)


编辑:感谢这里引用的C++标准(N33),第7.7.1(强调矿山):

1(…)类模板专门化的隐式实例化导致类成员函数、成员类、作用域成员枚举、静态数据成员和成员模板的声明隐式实例化,而不是定义或默认参数的隐式实例化;(…)

8类模板的隐式实例化不会导致该类的任何静态数据成员 隐式实例化

来自(我的):

如果编译器隐式实例化包含静态成员的类模板,则这些静态成员不会隐式实例化。仅当编译器需要静态成员的定义时,编译器才会实例化静态成员

因此,要强制编译器有效地实例化
GenTest::OnePerType
GenTest::OnePerType
,您必须以某种方式引用它们。例如,您可以在构造函数中添加
(void)&OnePerType;
(实时示例:)


编辑:感谢这里引用的C++标准(N33),第7.7.1(强调矿山):

1(…)类模板专门化的隐式实例化导致类成员函数、成员类、作用域成员枚举、静态数据成员和成员模板的声明隐式实例化,而不是定义或默认参数的隐式实例化;(…)

8类模板的隐式实例化不会导致该类的任何静态数据成员 隐式实例化


对于使用泛型类的每种类型,都会创建一个全新的类型。对于C++和C++,这是适用的。静态变量是跨类型的,而不是跨多个类型共享的。列表是一个完全不同于List.的类型。它如何应用到上面的例子?如果静态成员不能跨类型共享,那么为什么初始化?模板类型的一个外部定义可能是什么?这个初始化是什么意思?当它被忽略时,对于使用泛型类的每一个类型,都会创建一个全新的类型。这对于C++和C++来说是正确的。静态变量是跨类型的,而不是跨多个类型共享的。列表是一个完全不同的类型。类型大于列表。它如何应用于上面的示例?如果静态成员不能在类型之间共享,那么为什么可以在模板类的定义之外初始化OnePerType?当它似乎被忽略时,此初始化意味着什么?(它适用于gcc版本4.7.3)我忍不住觉得这有点愚蠢。别误会我,你的解释是准确的。问题本身看起来非常棘手:)(它与gcc版本4.7.3一起工作)我忍不住觉得这有点愚蠢。别误会我,你的解释是准确的。问题本身看起来非常棘手:)。