簡體   English   中英

C ++:枚舉類型作為模板參數 - 全局范圍

[英]C++: enum type as template argument - global scope

我有這種情況:

template<typename myEnumType>
int foo(const myEnumType & shortest_paths_algorithm)
{
    ...
}

int main()
{
    myEnumType enum_type_istance;
    int a = foo(enum_type_istance)
}

如果我宣布

typedef enum {AAA, BBB} myEnumType;

在函數聲明之前一切正常。 然而,如果我在創建enum_type_istance變量之前編寫上面的行,請獲取錯誤

沒有用於調用'foo(main():: myEnumType&)'候選者的匹配函數是:template int foo(const myEnumType&)

為什么??? 我怎樣才能在主內部進行類型定義? 謝謝!

您在C ++ 11之前使用C ++,它不允許在模板參數中使用“本地”類型。 幸運的是,這個特性已經在C ++ 11中引入。 正如你所看到的那樣 ,使用-std=c++11標志可以很好地編譯 ,而沒有它就會失敗

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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