C++ template specialization without default function

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

    The following are alternative techniques to using boost:

    Declare a typedef to a dependent name

    This works because name lookup for DONT only occurs when 'T' has been replaced. This is a similar (but legal) version of the example given by Kirill

    template 
    T GetGlobal (const char * name) {
        typedef typename T::DONT CALL_THIS_FUNCTION;
    }
    

    Use an incomplete return type

    This technique doesn't work for specializations, but it will work for overloads. The idea is that its legal to declare a function which returns an incomplete type, but not to call it:

    template 
    class DONT_CALL_THIS_FUNCTION GetGlobal (const char * name);
    

提交回复
热议问题