Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type specialization at compile-time

I'm writing a class which shares several different features with std::function (or at least the classes are in many ways similar). As you all know std::function is instantiated by specifying the template parameters (i.e std::function<void (std::string&)>), it is the same for my class. I have an exception though, I want to specialize a single function in my class, if the return value is void (std::function<"return value" ("parameters">). I need this to be done at compile time, and I just can't make it work as it should. Here is some test code for explanation:

#include <iostream>
#include <type_traits>

template <typename T> class Test { };

template <typename Ret, typename... Args>
class Test<Ret (Args...)>
{
public:
    Ret operator()(Args...)
    {
        if(std::is_void<Ret>::value)
        {
             // Do something...
        }

        else /* Not a void function */
        {
            Ret returnVal;
            return returnVal;
        }
    }
};

int main(int argc, char * argv[])
{
    Test<void (char)> test;
    test('k');
}

As you can clearly see, if the compiler does not remove the 'else' branch in the above test, my code will try to create a void value (i.e void returnVal;). The problem is that the compiler does not remove the branch so I end up with a compiler error:

./test.cpp: In instantiation of ‘Ret Test::operator()(Args ...) [with Ret = void; Args = {char}]’: ./test.cpp:27:10: required from here ./test.cpp:18:8: error: variable or field ‘returnVal’ declared void ./test.cpp:19:11: error: return-statement with a value, in function returning 'void' [-fpermissive]

One would normally use std::enable_if combined with std::is_void, the problem is that I don't want to specialize on the function template, but on the class template.

template <typename Ret, typename... Args>
class Test<Ret (Args...)>
{
public:
    typename std::enable_if<!std::is_void<Ret>::value, Ret>::type 
    Ret operator()(Args...)
    {
        Ret returnVal;
        return returnVal;
    }

    typename std::enable_if<std::is_void<Ret>::value, Ret>::type 
    Ret operator()(Args...)
    {
        // It's a void function
        // ...
    }
};

If I use the above code instead I end up with even more errors and without a solution

./test.cpp:11:2: error: expected ‘;’ at end of member declaration
./test.cpp:11:2: error: declaration of ‘typename std::enable_if<(! std::is_void<_Tp>::value), Ret>::type Test<Ret(Args ...)>::Ret’
./test.cpp:6:11: error:  shadows template parm ‘class Ret’
./test.cpp:11:24: error: ISO C++ forbids declaration of ‘operator()’ with no type [-fpermissive]
./test.cpp:18:2: error: expected ‘;’ at end of member declaration
./test.cpp:18:2: error: declaration of ‘typename std::enable_if<std::is_void<_Tp>::value, Ret>::type Test<Ret(Args ...)>::Ret’
./test.cpp:6:11: error:  shadows template parm ‘class Ret’
./test.cpp:18:24: error: ISO C++ forbids declaration of ‘operator()’ with no type [-fpermissive]
./test.cpp:18:6: error: ‘int Test<Ret(Args ...)>::operator()(Args ...)’ cannot be overloaded
./test.cpp:11:6: error: with ‘int Test<Ret(Args ...)>::operator()(Args ...)’
./test.cpp: In member function ‘int Test<Ret(Args ...)>::operator()(Args ...)’:
./test.cpp:22:2: warning: no return statement in function returning non-void [-Wreturn-type]
./test.cpp: In instantiation of ‘int Test<Ret(Args ...)>::operator()(Args ...) [with Ret = void; Args = {char}]’:
./test.cpp:28:10:   required from here
./test.cpp:13:7: error: variable or field ‘returnVal’ declared void
./test.cpp: In member function ‘int Test<Ret(Args ...)>::operator()(Args ...) [with Ret = void; Args = {char}]’:
./test.cpp:15:2: warning: control reaches end of non-void function [-Wreturn-type]

I'm sorry if I'm just plain dumb, and the answer is obvious. I'm fairly new to templates and I couldn't find a suiting answer in any of the other threads/questions.

like image 289
Elliott Darfink Avatar asked Jun 26 '12 14:06

Elliott Darfink


1 Answers

There are a few things that are not exactly clear from your description, so I will start with the most general answer.

Assuming that the template has other functions for which the behavior must be kept the same, and you only want to redefine the behavior for that particular function, the simplest answer is to split the template in two, and use inheritance to merge them. At this point you can use partial template specialization on the base template:

template <typename T, typename... Args>
struct tmpl_base {
   T operator()( Args... args ) {
       //generic
   }
};
template <typename... Args>
struct tmpl_base<void,Args...> {
   void operator()( Args... args ) {
   }
};

template <typename Ret, typename... Args>
class Test<Ret (Args...)> : tmp_base<Ret,Args...> {
   // do not declare/define operator(), maybe bring the definition into scope:
   using tmp_base<Ret,Args...>::operator();

   // Rest of the class

If this is the only function in your template, then partial specialization is a much simpler solution that does not require abusing inheritance.

like image 92
David Rodríguez - dribeas Avatar answered Oct 01 '22 20:10

David Rodríguez - dribeas