C++ template specialization without default function

后端 未结 5 1630
轻奢々
轻奢々 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:12

    Though it is an old and outdated question, it may worth noting that C++11 had solved this issue using deleted functions:

    template
    T GetGlobal(const char *name) = delete;
    
    template<>
    int GetGlobal(const char *name);
    

    UPDATE

    This will not compile under MacOS llvm 8. It is due to a still hanging 4 years old defect (see this bug report).

    The following workaround will fit the issue (using a static_assert construct).

    template
    T GetGlobal(const char *name) {
        static_assert(sizeof(T) == 0, "Only specializations of GetGlobal can be used");
    }
    
    template<>
    int GetGlobal(const char *name);
    

    UPDATE

    Visual studio 15.9 has the same bug. Use the previous workaround for it.

提交回复
热议问题