Boost C++ Libraries Home Libraries People FAQ More


Type Requirements and User-defined-types support 類型要求和用戶自定義類型支持

Type Requirements 類型要求
UDT's special semantics 用戶自定義類型的特殊語義
Special Policies 特殊策略

Both arithmetic (built-in) and user-defined numeric types require proper specialization of std::numeric_limits<> (that is, with (in-class) integral constants).
算術類型(內建的)和用戶自定義的數字類型都要求對 std::numeric_limits<> (以(類中)的整型常量)進行正確的特化。

The library uses std::numeric_limits<T>::is_specialized to detect whether the type is builtin or user defined, and std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed to detect whether the type is integer or floating point; and whether it is signed/unsigned.
本庫用 std::numeric_limits<T>::is_specialized 來檢測某個類型是否內建的或用戶定義的,用 std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed 來檢測某個類型為整數或浮點數;以及是否有符號/無符號。

The default Float2IntRounder policies uses unqualified calls to functions floor() and ceil(); but the standard functions are introduced in scope by a using directive:
缺省的 Float2IntRounder 策略以非受限方式調用函數 floor()ceil();而標準函數是通過一個 using 指令引入到作用域中:

using std::floor ; return floor(s);

Therefore, for builtin arithmetic types, the std functions will be used. User defined types should provide overloaded versions of these functions in order to use the default rounder policies. If these overloads are defined within a user namespace argument dependent lookup (ADL) should find them, but if your compiler has a weak ADL you might need to put these functions some place else or write your own rounder policy.
因此,對於內建的算術類型,將使用標準函數。為了使用缺省的捨入策略,用戶自定義類型應該提供這些函數的重載版本。如果這些重載是在一個用戶名字空間內定 義的,參數依賴查找(ADL)會找到它們,但是如果你的編譯器的ADL較弱,那麼你可能需要將這些函數放在其它地方,或者編寫你自己的捨入策略。

The default Trunc<> rounder policy needs to determine if the source value is positive or not, and for this it evaluates the expression s < static_cast<S>(0). Therefore, user defined types require a visible operator< in order to use the Trunc<> policy (the default).
缺省的 Trunc<> 捨入策略需要判斷源數值是否為正,為此它要對表達式 s < static_cast<S>(0) 進行求值。因此,為了使用 Trunc<> 策略(缺省情況),用戶自定義類型需要一個可見的 operator<

Conversion Traits 轉換 Traits

If a User Defined Type is involved in a conversion, it is assumed that the UDT has wider range than any built-in type, and consequently the values of some converter_traits<> members are hardwired regardless of the reality. The following table summarizes this:
在轉換中,如果涉及到用戶自定義類型,將 假定 UDT 具有比任意內建類型 更 廣的範圍,因此不管事實如何,有些 converter_traits<> 成員就是硬性規定的。總結如下表:

  • Target=UDT and Source=built-in
    • subranged=false
    • supertype=Target
    • subtype=Source
  • Target=built-in and Source=UDT
    • subranged=true
    • supertype=Source
    • subtype=Target
  • Target=UDT and Source=UDT
    • subranged=false
    • supertype=Target
    • subtype=Source

The Traits member udt_mixture can be used to detect whether a UDT is involved and to infer the validity of the other members as shown above.
Traits 成員 udt_mixture 可用於檢測是否涉及某個 UDT 並如上所示推斷其它成員的有效性。

Range Checking 範圍檢查

Because User Defined Numeric Types might have peculiar ranges (such as an unbounded range), this library does not attempt to supply a meaningful range checking logic when UDTs are involved in a conversion. Therefore, if either Target or Source are not built-in types, the bundled range checking of the converter<> function object is automatically disabled. However, it is possible to supply a user-defined range-checker. See Special Policies
因為用戶自定義數字類型可能具有特殊的範圍(如一個無界的範圍),因此本庫在轉換涉及某個UDT時不提供一個有意義的範圍檢查邏輯。因此,如果目標類型或源類型之一不是內建類型,則 converter<> 函數對像所綁定的範圍檢查就會自動禁用。不過,你可以提供一個用戶自定義的範圍檢查器。請見 特殊策略

There are two components of the converter<> class that might require special behavior if User Defined Numeric Types are involved: the Range Checking and the Raw Conversion.
如果涉及到用戶自定義類型,converter<> 類中有兩個組件可能需要特殊的行為:範圍檢查和原始轉換。

When both Target and Source are built-in types, the converter class uses an internal range checking logic which is optimized and customized for the combined properties of the types.

However, this internal logic is disabled when either type is User Defined. In this case, the user can specify an external range checking policy which will be used in place of the internal code. See UserRangeChecker policy for details.
但是,當有其中一個類型是用戶自定義類型時,該內部邏輯就會被禁用。這種情況下,用戶可以指定一個 外部 的範圍檢查策略,用於替換內部代碼。具體請見 UserRangeChecker 策略。

The converter class performs the actual conversion using a Raw Converter policy. The default raw converter simply performs a static_cast<Target>(source).
轉換器類使用一個原始轉換器策略執行實際的轉換。缺省的原始轉換器只是執行一個 static_cast<Target>(source).

However, if the a UDT is involved, the static_cast might not work. In this case, the user can implement and pass a different raw converter policy. See RawConverter policy for details
但是,如果涉及到 UDT,static_cast 也許就不能奏效了。這種情況下,用戶可以實現一個不同的原始轉換器策略並傳入它。具體請見 RawConverter 策略。