我想我现在明白了。这是一个示例模拟:
#include <boost/static_assert.hpp>
#include <boost/type_traits.hpp>
#include <boost/utility/enable_if.hpp>
#include <typeinfo>
#include <iostream>
template<class TT, typename Enable = void> struct UU { BOOST_STATIC_ASSERT_MSG(sizeof(TT) == 0, "undefined UU"); };
template<class TT>
struct UU<
TT,
typename boost::enable_if_c<
!boost::is_same<
TT,
typename boost::remove_cv<
typename boost::decay<TT>::type
>::type
>::value
|| boost::is_pointer<TT>::value
>::type
>
: UU<
typename boost::remove_cv<
typename boost::decay<
typename boost::remove_pointer<TT>::type
>::type
>::type
>
{
UU()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
struct dummy
{
dummy()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
template<>
struct UU<dummy>
{
UU()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
int main()
{
typedef dummy**& mytype;
std::cout << typeid(typename boost::remove_cv<typename boost::decay<mytype>::type>::type).name() << std::endl;
std::cout << boost::is_pointer<mytype>::value << std::endl;
std::cout << boost::is_same<mytype, typename boost::remove_cv<typename boost::decay<mytype>::type>::type>::value << std::endl;
UU<mytype> uu;
return 0;
}
运行时:
me@ub16:~/tmp/traits$ g++ -I~/me/bin/boost_1_60_0 c.cpp && ./a.out
PP5dummy
0
0
UU<dummy>::UU()
UU<TT, typename boost::enable_if_c<((! boost::is_same<TT, typename boost::remove_cv<typename boost::decay<T>::type>::type>::value) || boost::is_pointer<T>::value)>::type>::UU() [with TT = dummy*; typename boost::enable_if_c<((! boost::is_same<TT, typename boost::remove_cv<typename boost::decay<T>::type>::type>::value) || boost::is_pointer<T>::value)>::type = void]
UU<TT, typename boost::enable_if_c<((! boost::is_same<TT, typename boost::remove_cv<typename boost::decay<T>::type>::type>::value) || boost::is_pointer<T>::value)>::type>::UU() [with TT = dummy**; typename boost::enable_if_c<((! boost::is_same<TT, typename boost::remove_cv<typename boost::decay<T>::type>::type>::value) || boost::is_pointer<T>::value)>::type = void]
UU<TT, typename boost::enable_if_c<((! boost::is_same<TT, typename boost::remove_cv<typename boost::decay<T>::type>::type>::value) || boost::is_pointer<T>::value)>::type>::UU() [with TT = dummy**&; typename boost::enable_if_c<((! boost::is_same<TT, typename boost::remove_cv<typename boost::decay<T>::type>::type>::value) || boost::is_pointer<T>::value)>::type = void]
me@ub16:~/tmp/traits$
当UU<dummy**&> 被实例化时,编译器会尝试创建/定义struct UU<dummy**>。然后它尝试创建struct UU<dummy*>。最后,它创建struct UU<dummy>。
当部分模板特化struct UU<dummy>未定义时,主模板的静态断言失败:
me@ub16:~/tmp/traits$ g++ -I~/me/bin/boost_1_60_0 c.cpp && ./a.out
In file included from c.cpp:1:0:
c.cpp: In instantiation of ‘struct UU<dummy, void>’:
c.cpp:10:8: recursively required from ‘struct UU<dummy**, void>’
c.cpp:10:8: required from ‘struct UU<dummy**&>’
c.cpp:62:13: required from here
c.cpp:7:56: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
template<class TT, typename Enable = void> struct UU { BOOST_STATIC_ASSERT_MSG(sizeof(TT) == 0, "undefined UU"); };
本质上,代码作者希望其他开发人员将使用他/她的代码来为要使用的每个 TT 定义部分模板特化 template<> struct UU<TT>。