Re: [cpp] 关于模板

Chen Shuo giantchen at gmail.com
Mon Jul 31 15:39:53 CST 2006


模板函数在用到的时候才会instantiation.

On 7/31/06, ling <xl-yc-lzj at necsoft.com.cn> wrote:
>
> c++编程思想的一道习题,下面的代码中,
> HardLogic的compare函数会报错,但是SoftLogic的
> compare函数却不报错,这是为什么呢?
>
> class Noncomparable { };
>
> struct HardLogic {
>  Noncomparable nc1, nc2;
>  void compare() {
>   return nc1 = nc2;
>  }
> };
>
> template<class T> struct SoftLogic {
>  T nc1, nc2;
>  void noOp() {}
>  void compare() {
>   return nc1 = nc2;
>  }
> };
>
> int main()
> {
>  SoftLogic<Noncomparable> l;
>  l.noOp();
> }
>
> _______________________________________________
> Cpp mailing list
> Cpp at codingnow.com
> http://codingnow.com/mailman/listinfo/cpp
>
>



More information about the Cpp mailing list