問題描述
我剛剛問了這個問題:std::numeric_limits 作為條件>
我了解 std::enable_if
將有條件地定義方法的返回類型導致方法編譯失敗的用法.
template類型名稱 std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar);}
我不明白的是第二個參數和對 std::enable_if
的看似毫無意義的賦值,當它被聲明為模板語句的一部分時,如 Rapptz answer.
template::value, int>::type = 0>void foo(const T& bar) { isInt();}
正如 40two 在評論中提到的,理解替換失敗不是錯誤是理解的先決條件std::enable_if
.
std::enable_if
是一個專門的模板,定義為:
template結構 enable_if {};模板struct enable_if{ typedef T 類型;};
這里的關鍵在于typedef T type
僅在bool Cond
為true
時才定義.
現在有了對 std::enable_if
的理解,很明顯 void foo(const T &bar) { isInt(bar);}
定義為:
template類型名稱 std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar);}
如firda 的答案所述,= 0
是第二個模板的默認值范圍.template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
中默認的原因是兩個選項都可以用 foo<調用int >( 1 );
.如果 std::enable_if
模板參數沒有被默認,調用 foo
將需要兩個模板參數,而不僅僅是 int
.
一般注意,通過明確輸入 typename std::enable_if
但 void
是 std::enable_if
的默認第二個參數,如果你有 c++14 enable_if_t
是一個定義的類型,應該使用.所以返回類型應該壓縮為:std::enable_if_t
給 visual- 的用戶的特別說明-工作室 visual-studio-2013:不支持默認模板參數,因此您只能在函數返回時使用 enable_if
:std::numeric_limits 作為條件
I just asked this question: std::numeric_limits as a Condition
I understand the usage where std::enable_if
will define the return type of a method conditionally causing the method to fail to compile.
template<typename T>
typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar); }
What I don't understand is the second argument and the seemingly meaningless assignment to std::enable_if
when it's declared as part of the template statement, as in Rapptz answer.
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
void foo(const T& bar) { isInt(); }
As is mentioned in comment by 40two, understanding of Substitution Failure Is Not An Error is a prerequisite for understanding std::enable_if
.
std::enable_if
is a specialized template defined as:
template<bool Cond, class T = void> struct enable_if {};
template<class T> struct enable_if<true, T> { typedef T type; };
The key here is in the fact that typedef T type
is only defined when bool Cond
is true
.
Now armed with that understanding of std::enable_if
it's clear that void foo(const T &bar) { isInt(bar); }
is defined by:
template<typename T>
typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar); }
As mentioned in firda's answer, the = 0
is a defaulting of the second template parameter. The reason for the defaulting in template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
is so that both options can be called with foo< int >( 1 );
. If the std::enable_if
template parameter was not defaulted, calling foo
would require two template parameters, not just the int
.
General note, this answer is made clearer by explicitly typing out typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type
but void
is the default second parameter to std::enable_if
, and if you have c++14 enable_if_t
is a defined type and should be used. So the return type should condense to: std::enable_if_t<std::numeric_limits<T>::is_integer>
A special note for users of visual-studio prior to visual-studio-2013: Default template parameters aren't supported, so you'll only be able to use the enable_if
on the function return: std::numeric_limits as a Condition
這篇關于std::enable_if 如何工作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!