簡體   English   中英

可變參數模板“對重載函數的模糊調用”似乎是一個錯誤的錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM