Force type of C++ template

前端 未结 6 1276
难免孤独
难免孤独 2020-12-28 21:34

I\'ve a basic template class, but I\'d like to restrain the type of the specialisation to a set of classes or types. e.g.:

template 
class          


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 21:46

    Im not sure about this, but you could add another template specialization for double template

    class MyClass
    {
    .../...
    private:
        T* _p;
    };
    
    template  class MyClass
    {};
    

    which would work for your example, but not for the general case.

    In general, I would add a compile assert to check for unwanted types.

    Hope it helps.

提交回复
热议问题