從boost::units
:使用CRTP技術,確保獨特的模板參數
struct my_base_dimension1 : units::base_dimension<my_base_dimension1, 1> { }; // ok
struct my_base_dimension2 : units::base_dimension<my_base_dimension2, 2> { }; // ok
struct my_base_dimension3 : units::base_dimension<my_base_dimension3, 2> { }; // error
我試圖理解代碼如何確保模板參數是唯一的。我不明白check_base_dimension
如何最終返回一個非零值,這會觸發ordinal_has_already_been_defined<true>
。我認爲它與在base_dimension
重新定義有關,但我不知道如何調用friend
版本。有任何想法嗎?
有問題的文件是base_dimension.hpp
和prevent_redefinition.hpp
。
template<class Derived, long N,
class = typename detail::ordinal_has_already_been_defined<
check_base_dimension<Derived, N>::value
>::type
>
class base_dimension : public ordinal<N>
{
public:
typedef base_dimension this_type;
typedef list<dim<Derived,static_rational<1> >, dimensionless_type> dimension_type;
typedef Derived type;
private:
friend Derived*
check_double_register(const units::base_dimension_ordinal<N>&)
{ return(0); }
friend detail::yes
boost_units_is_registered(const units::base_dimension_ordinal<N>&)
{ detail::yes result; return(result); }
friend detail::yes
boost_units_is_registered(const units::base_dimension_pair<Derived, N>&)
{ detail::yes result; return(result); }
};
你知道'detail :: yes'和'detail :: no'的定義在哪裏嗎?我一直試圖通讀Boost的源代碼,但我認爲我和你一樣困惑。 – KRyan 2012-08-02 22:33:36
@DragoonWraith'detail :: yes'和'detail :: no'位於'prevent_redefinition.hpp'文件中。 – Arlen 2012-08-02 22:48:13
所以他們是!我錯過了錯過他們的感覺。嗯......好吧,我會繼續看,如果我得到它。 – KRyan 2012-08-02 22:49:04