Is there any difference between “T” and “const T” in template parameter?

后端 未结 3 964
借酒劲吻你
借酒劲吻你 2020-12-03 20:56

Is there any difference between following 2 syntax:

template struct A;         // (1)

and

template

        
3条回答
  •  无人及你
    2020-12-03 21:19

    The choice of int was probably a bad idea, it makes a difference for pointers though:

    class A
    {
    public:
        int Counter;
    };
    
    A a;
    
    
    template 
    struct Coin
    {
        static void DoStuff()
        {
            ++a->Counter; // won't compile if using const A* !!
        }
    };
    
    Coin<&a>::DoStuff();
    cout << a.Counter << endl;
    

提交回复
热议问题