[英]variadic template 'ambiguous call to overloaded function' seems a false error
我尝试编写一个模板函数来初始化给定的系统并运行应用程序,最后在初始化的系统上运行关闭函数。
这段代码在我看来应该可以工作,并且智能感知不会给出任何错误,但编译器:
1>C:\VisualStudio\DirectApp\AppMain.cpp(32,2): error C2668: 'initialize_these': ambiguous call to overloaded function
1>C:\VisualStudio\DirectApp\AppMain.cpp(28,13): message : could be 'void initialize_these<WindowManager,>(void)'
1>C:\VisualStudio\DirectApp\AppMain.cpp(18,13): message : or 'void initialize_these<WindowManager>(void)'
1>C:\VisualStudio\DirectApp\AppMain.cpp(29,1): message : while trying to match the argument list '()'
1>C:\VisualStudio\DirectApp\AppMain.cpp(29): message : see reference to function template instantiation 'void initialize_these<Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(29): message : see reference to function template instantiation 'void initialize_these<Heap,Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(29): message : see reference to function template instantiation 'void initialize_these<SystemInfo,Heap,Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.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:\VisualStudio\DirectApp\AppMain.cpp(22,2): error C2672: 'initialize_these': no matching overloaded function found
1>C:\VisualStudio\DirectApp\AppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<WindowManager,>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<Heap,Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(19): message : see reference to function template instantiation 'void initialize_these<SystemInfo,Heap,Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(35): message : see reference to function template instantiation 'void initialize_these<Sdl,SystemInfo,Heap,Log,WindowManager>(void)' being compiled
1>C:\VisualStudio\DirectApp\AppMain.cpp(22,2): error C2783: 'void initialize_these(void)': could not deduce template argument for 'First'
1>C:\VisualStudio\DirectApp\AppMain.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++17
if constexpr
你也可以这样写
template<class Type, class... Rest>
static void f(){
// do something with Type
if constexpr(sizeof...(Rest))
f<Rest...>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.