2016-07-08 26 views
0

我意識到在觸及這個主題時出現了一些問題,但我還沒有找到解決方案。從屬名稱T被解析爲非類型,但實例化產生一個類型

我只想在函數f的所有參數都是pod時才啓用它。

,我有以下的代碼要做到這一點:

#include <type_traits> 

template <typename... Conds> 
struct and_ : std::true_type 
{ 
}; 

template <typename Cond, typename... Conds> 
struct and_<Cond, Conds...> : 
    std::conditional<Cond::value, and_<Conds...>, std::false_type>::type 
{ 
}; 

template <typename... T> 
using are_all_pod = and_<std::is_pod<T>...>; 

template<typename... Args, 
     typename = typename std::enable_if<are_all_pod<Args...>::type>> 
void f(Args ... args) 
{ 
} 

int main() 
{ 
    f(1,2); 
} 

我收到以下錯誤:

main.cpp: In function ‘int main()’: 
main.cpp:25:8: error: no matching function for call to ‘f(int, int)’ 
    f(1,2); 
     ^
main.cpp:19:6: note: candidate: template<class ... Args, class> void f(Args ...) 
void f(Args ... args) 
    ^
main.cpp:19:6: note: template argument deduction/substitution failed: 
main.cpp:18:10: error: dependent-name ‘and_<std::is_pod<T>...>::type’ is parsed as a non-type, but instantiation yields a type 
      typename = typename std::enable_if<are_all_pod<Args...>::type>> 
     ^
main.cpp:18:10: note: say ‘typename and_<std::is_pod<T>...>::type’ if a type is meant 

我嘗試以下編譯器的建議,但我得到同樣的錯誤,然後。使用gcc 5.2.1

+0

「我得到同樣的錯誤,然後」 - 完全相同的錯誤? – ecatmur

+0

那麼你想要做什麼 –

+0

@LightnessRacesinOrbit請檢查我的編輯 – Patryk

回答

2

std::enable_if<are_all_pod<Args...>::type>是沒有意義的。

將其更改爲類似std::enable_if<are_all_pod<Args...>::value, void>::type的程序將編譯。

+0

顯然...對不起我的壞,謝謝你指出這一點! – Patryk

相關問題