Question

I have code:

template<typename T>
void loadBrush_sub_impl()
{
    // do some work here
}

template<typename T, typename... Targs>
void loadBrush_sub()
{
    loadBrush_sub_impl<T>();
    loadBrush_sub<Targs...>();
}

template<>
void loadBrush_sub<void>()
{
}

// BasicBrush, BinaryBrush, SketchBrush, BasicEraser and MaskBased are class
loadBrush_sub<BasicBrush, BinaryBrush, SketchBrush, BasicEraser, MaskBased, void>();

This is correct when compiling it. However, I really want to drop the void in the call loadBrush_sub<BasicBrush, BinaryBrush, SketchBrush, BasicEraser, MaskBased, void>();.

However, this leads to:

..\CanvasEngine\canvasengine.cpp: In instantiation of 'void loadBrush_sub() [with T = MaskBased; Targs = {}]':
..\CanvasEngine\canvasengine.cpp:36:5:   recursively required from 'void loadBrush_sub() [with T = BinaryBrush; Targs = {SketchBrush, BasicEraser, MaskBased}]'
..\CanvasEngine\canvasengine.cpp:36:5:   required from 'void loadBrush_sub() [with T = BasicBrush; Targs = {BinaryBrush, SketchBrush, BasicEraser, MaskBased}]'
..\CanvasEngine\canvasengine.cpp:114:81:   required from here
..\CanvasEngine\canvasengine.cpp:36:5: error: no matching function for call to 'loadBrush_sub()'
..\CanvasEngine\canvasengine.cpp:36:5: note: candidate is:
..\CanvasEngine\canvasengine.cpp:33:6: note: template<class T, class ... Targs> void loadBrush_sub()
..\CanvasEngine\canvasengine.cpp:33:6: note:   template argument deduction/substitution failed:
..\CanvasEngine\canvasengine.cpp:36:5: note:   couldn't deduce template parameter 'T'
mingw32-make[1]: *** [release/canvasengine.o] Error 1

I ran some experiments with enable_if, but no luck.

Is there any solution to drop the void and make the compiler happy?

Was it helpful?

Solution 2

The easiest solution is adding another indirection:

template<typename T>
void loadBrush_sub_impl()
{
    // do some work here
}

template<typename... Targs>
void loadBrush_sub();

template<typename T, typename... V>
void loadBrush_sub_helper()
{
    loadBrush_sub_impl<T>();
    loadBrush_sub<V...>();
}

template<typename... Targs>
void loadBrush_sub()
{
    loadBrush_sub_helper<Targs...>();
}

template<>
void loadBrush_sub<>()
{
}

OTHER TIPS

Without helper (notice "= void"):

template<typename T>
void loadBrush_sub_impl()
{
    // do some work here
}

template<typename T = void, typename... Targs>
void loadBrush_sub();

template<typename T, typename... Targs>
void loadBrush_sub()
{
    loadBrush_sub_impl<T>();
    loadBrush_sub<Targs...>();
}

template<>
void loadBrush_sub<>()
{
}

You might specialize a helper structure:

#include <iostream>

namespace Detail {
    template<typename T, typename... Targs>
    struct LoadBrush;

    template<typename T>
    struct LoadBrush<T> {
        static void sub() {
            std::cout << "Work\n";
        }
    };

    template<typename T, typename... Targs>
    struct LoadBrush {
        static void sub() {
            LoadBrush<T>::sub();
            LoadBrush<Targs...>::sub();
        }
    };
}

template<typename... Targs>
void loadBrush_sub()
{
    Detail::LoadBrush<Targs...>::sub();
}


struct BasicBrush {};
struct BinaryBrush {};
struct SketchBrush {};
struct BasicEraser {};
struct MaskBased {};

int main()
{
    loadBrush_sub<BasicBrush, BinaryBrush, SketchBrush, BasicEraser, MaskBased>();
    return 0;
}

the other way is to use SFINAE.

#include <type_traits>
template<typename T>
void loadBrush_sub_impl()
{
    // do some work here
}

template<typename T>
void loadBrush_sub()
{
    loadBrush_sub_impl<T>();
}

template<typename T, typename... Ts>
std::enable_if_t<(sizeof...(Ts) > 0), void> 
loadBrush_sub()
{
    loadBrush_sub_impl<T>();
    loadBrush_sub<Ts...>();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top