C++ template specialization without default function

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

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

template<>
int GetGlobal<int>(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<typename T>
T GetGlobal(const char *name) {
    static_assert(sizeof(T) == 0, "Only specializations of GetGlobal can be used");
}

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

UPDATE

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)