`
gaofen100
  • 浏览: 1187516 次
文章分类
社区版块
存档分类
最新评论

C++ template static variable init

 
阅读更多

下面代码的输出结果

#include <iostream>
using namespace std;

int g=0;

template<typename T>
int foo()
{
static int value = ++g;
return value;
}

int main(int argc, char* argv[])
{
cout<<foo<int>()<<foo<bool>()<<foo<float>()<<foo<bool>()<<foo<int>()<<foo<char>();

cout<<endl;
return 0;
}

>> ./a.out
123214

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics