function template in non class template. Different class or Overloaded?

 

非类模板中的函数模板在实际生成的时候, 会因为其函数模板生成不同的类呢还是会把类中函数模板当作同一函数的重载呢?

 

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class T
{
public:
T(){}
~T(){}
T(const T &other){}
T &operator=(const T &other){ return *this; }
public:
// Interface
template <typename S>
void foo(S s)
{
cout <<"foo()" <<endl;
}
};

int _tmain(int argc, _TCHAR* argv[])
{
T t1, t2;
t1.foo(1);
t2.foo("1");
t2 = t1; // Ok, it means that T::foo() is overloaded function. Bacause
// two different class will not support assignment without
// convertion.
return 0;
}

 

因此, 我们粗略的得出了一个结论, 就是在实际编译的时候, 编译器只生成了一个类, 其中的函数模板 foo 生成了同一函数的不同重载形式.

 

posted @ 2012-02-14 14:15  walfud  阅读(183)  评论(0编辑  收藏  举报