gpt4 book ai didi

c++ - 如何匹配可变参数模板中的空参数包

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:48 24 4
gpt4 key购买 nike

我有代码:

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>();

编译时这是正确的。但是,我真的很想删除 void在通话中loadBrush_sub<BasicBrush, BinaryBrush, SketchBrush, BasicEraser, MaskBased, void>(); .

但是,这会导致:

..\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

我用 enable_if 做了一些实验, 但没有运气。

是否有任何解决方案可以删除 void让编译器开心?

最佳答案

最简单的解决方案是添加另一个间接寻址:

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<>()
{
}

关于c++ - 如何匹配可变参数模板中的空参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com