Template class, function specialization

后端 未结 3 1306
旧巷少年郎
旧巷少年郎 2021-02-09 13:40

I want to have a template class that looks something like what I have down below. Then, I want a function in it with a template specialization depending on a CLASS template para

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-09 14:29

    struct Otherwise { };
    template struct C : Otherwise { };
    
    // don't use _Uppercase - those names are reserved for the implementation
    // (i removed the '_' char)
    template 
    class Foo
    {
    public:
        void Func() { Func(C()); }
    
    private:
        // If num == 1, I want to call this function...
        void Func(C<1>)
        {
            printf("Hi 1!");
        }
    
        // If num == 2, I want to call this function...
        void Func(C<2>)
        {
            printf("Hi 2!");
        }
    
        // Otherwise, I want to call this version.
        void Func(Otherwise)
        {
            printf("Hello world!");
        }
    
        //// alternative otherwise solution:
        // template
        // void Func(C) { .. }
    };
    

提交回复
热议问题