可变参数模板'ambiguous call to overloaded function'似乎是错误



我试图编写一个模板函数来初始化给定的系统并运行应用程序,最后在初始化的系统上运行关闭函数。

这段代码在我看来应该可以工作,intellisense不会给出任何错误,但编译器:

1>C:VisualStudioDirectAppAppMain.cpp(32,2): error C2668: 'initialize_these': ambiguous call to overloaded function
1>C:VisualStudioDirectAppAppMain.cpp(28,13): message : could be 'void initialize_these<WindowManager,>(void)'
1>C:VisualStudioDirectAppAppMain.cpp(18,13): message : or       'void initialize_these<WindowManager>(void)'
1>C:VisualStudioDirectAppAppMain.cpp(29,1): message : while trying to match the argument list '()'
1>C:VisualStudioDirectAppAppMain.cpp(29): message : see reference to function template instantiation 'void initialize_these<Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(29): message : see reference to function template instantiation 'void initialize_these<Heap,Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(29): message : see reference to function template instantiation 'void initialize_these<SystemInfo,Heap,Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(45): message : see reference to function template instantiation 'void initialize_these<Sdl,SystemInfo,Heap,Log,WindowManager>(void)' being compiled

代码:

#include "pch.h"
#include "AppMain.h"
#include "AppRun.h"
#include "Dummy.h"
#include "Heap.h"
#include "Log.h"
#include "Sdl.h"
#include "SystemInfo.h"
#include "WindowManager.h"
static void initialize_these()
{
AppMain::run<AppRun>();
}
template<class Type = Dummy>
static void initialize_these()
{
if (AppMain::initialize<Type>()) { return; }
initialize_these();
AppMain::shutdown<Type>();
}
template<class  First, class... Rest>
static void initialize_these()
{
if (AppMain::initialize<First>()) { return; }
initialize_these<Rest...>();
AppMain::shutdown<First>();
}
void AppMain::app_main()
{
initialize_these<
Sdl,
SystemInfo,
Heap,
Log,
WindowManager
>();
}

希望代码足够清晰。

哦,顺便说一句,如果初始化失败,那些AppMain::initialize<>()调用将为true。我检查这个代码的不同部分。在我看来,除了static void initialize_these()函数的可变模板之外,一切都还好。

编辑:我删除

template<class Type = Dummy>
static void initialize_these()
{
if (AppMain::initialize<Type>()) { return; }
initialize_these();
AppMain::shutdown<Type>();
}

编译器错误:

1>C:VisualStudioDirectAppAppMain.cpp(22,2): error C2672: 'initialize_these': no matching overloaded function found
1>C:VisualStudioDirectAppAppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<WindowManager,>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<Heap,Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<SystemInfo,Heap,Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(35): message : see reference to function template instantiation 'void initialize_these<Sdl,SystemInfo,Heap,Log,WindowManager>(void)' being compiled
1>C:VisualStudioDirectAppAppMain.cpp(22,2): error C2783: 'void initialize_these(void)': could not deduce template argument for 'First'
1>C:VisualStudioDirectAppAppMain.cpp(18): message : see declaration of 'initialize_these'

Rest可以为空,因此两者都有效。


您可以使变差一接受2个或多个参数

template<class Type>
static void f(){
// do something with Type
}
template<class First, class Second, class... Rest>
static void f(){
// do something with First
f<Second,Rest...>();
}

使用c++17if constexpr,您也可以像这样编写

template<class Type, class... Rest>
static void f(){
// do something with Type

if constexpr(sizeof...(Rest))
f<Rest...>();
}

相关内容

最新更新