我想用兩種模板型別撰寫一個模板化函式:一種是 bool,一種是型別名,我想專門研究型別名。
例如,這就是我想要的,但專門針對 T 的幾種型別:
template<bool b, typename T>
void foo(T val)
{
// do different stuff based on b and type of T.
}
沒有 bool 在那里,我可以做這樣的事情:
template<typename T>
void bar(T val) {
static_assert(false, "not implemented");
}
template<>
void bar<short>(short val) {
printf("it's a short!\n");
}
我無法弄清楚這個的語法,微軟的專業化檔案只涵蓋了單一型別的情況。
uj5u.com熱心網友回復:
template<bool B, typename T>
void foo(T const&)
{
static_assert(false, "not implemented");
}
template<bool B>
void foo(short)
{
printf("it's a short!\n");
}
然而,這并不是真正的專業化,而是多載,這是完全合適的。事實上,你可以省略一般情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361055.html
下一篇:使用復選框切換svg圓圈顏色
