我想知道when
專業化是如何在boost::hana::when<false>
案例沒有案例基礎的情況下工作的。boost :: hana tag_of implementation
boost::hana::tag_of
實現:
template<bool condition>
struct when; // forward declaration only
template<typename T, typename = void>
struct tag_of;
template<typename T, typename>
struct tag_of : tag_of<T, when<true> >
{};
template<typename T, bool condition>
struct tag_of<T, when<condition> >
{
using type = T;
};
和測試例如:
struct my_tag {};
struct my_tag2 {};
namespace boost {
namespace hana {
template<class T>
struct tag_of<T, when<std::is_same<T, int>{}()> >
{
using type = my_tag;
};
template<class T>
struct tag_of<T, when<std::is_same<T, unsigned>{}()> >
{
using type = my_tag2;
};
}
}
int main()
{
using type = boost::hana::tag_of<int>::type;
std::cout << std::is_same<type, my_tag>{} << std::endl;
}
,我不知道爲什麼std::is_same<T, int>{}()
(或::value
這是相同的),是一個更專業的偏特比std::is_same<T, unsigned>{}()
,以及爲什麼,如果兩種情況的條件都是錯誤的,when<condition>
更專業。
我已經完成了許多元函數,並且使用了專業化和參數包以及排序,但在這種情況下,有些東西我看不到。
的事情是,我不明白爲什麼when
模板的true
或false
值能夠決定的事情,如果有對false
情況下,沒有默認實現。
這是驚人的美麗。謝謝。 –