C++ template specialization without default function

后端 未结 5 1706
轻奢々
轻奢々 2021-02-01 02:49

I have the following code that compiles and works well:

template
T GetGlobal(const char *name);

template<>
int GetGlobal(cons         


        
5条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 03:25

    If you don't implement it, you'll at least get a linker error. If you want a compile-time error, you could do this with class templates:

    template
    struct GlobalGetter;
    
    template<>
    struct GlobalGetter {
      static int GetGlobal(const char *name);
    };
    
    template<>
    struct GlobalGetter {
      static double GetGlobal(const char *name);
    };
    
    template
    T GetGlobal(const char *name)
    {
      return GlobalGetter::GetGlobal(name);
    }
    

提交回复
热议问题