0

クラスのテンプレート パラメーターの型に基づいて代入演算子を提供するテンプレート整数ラッパー クラスを作成しています。

template<typename IntType>
class secure_int {
public:
  // enable only if boost::is_signed<IntType>
  secure_int &operator=(intmax_t value) {
   // check for truncation during assignment
  }

  // enable only if boost::is_unsigned<IntType>
  secure_int &operator=(uintmax_t value) {
   // check for truncation during assignment
  }
};

operator= はメンバー テンプレートではないため、boost::enable_if_c を使用した SFINAE は機能しません。そのような機能を提供するための作業オプションは何ですか?

4

2 に答える 2

2

テンプレートの特殊化を使用しないのはなぜですか?

template<typename IntT>
struct secure_int {};

template<>
struct secure_int<intmax_t>
{
  secure_int<intmax_t>& operator=(intmax_t value)
  { /* ... */ }
};

template<>
struct secure_int<uintmax_t>
{
  secure_int<uintmax_t>& operator=(uintmax_t value)
  { /* ... */ }
};
于 2012-02-19T07:48:18.967 に答える
1

これをメンバー テンプレートにして、C++11 でパラメータをデフォルトで void にすることができます。その機能をサポートするコンパイラがない場合は、特殊化が唯一のオプションです。

于 2012-02-19T07:47:50.790 に答える