0

次のようなデフォルトの引数static_castを持つコンストラクターが欲しいです。

generate_word_spot(func_double_double_t& cost_f = static_cast<func_double_double_t&>(func_const_t(1))) :
      cost_f_(cost_f)
      {};

どこ

class func_const_t : public func_double_double_t 
{ 
   ...
   virtual double operator()(double x){ ... }; 
}

これにfunc_double_double_t似た多くの関数オブジェクトの基本クラスです。

GCCはstatic_cast、上記のコンストラクターに対して「無効」と言います。そのような振る舞いを実現する方法はありますか?

4

1 に答える 1

1

あなたの場合、非定数参照が必要ですか?const参照を使用できる場合は、

generate_word_spot(const func_double_double_t& cost_f = func_const_t(1)) :
  cost_f_(cost_f)
  {}

キャストは必要ありません。(どちらも;定義の後ではありません。)

それ以外の場合、非定数参照バインディングの場合、一時オブジェクトは問題外です。デフォルトの引数として使用するには、スタンドアロンの非一時オブジェクトを宣言する必要があります

func_const_t def_const(1);
...
class generate_word_spot {
  ...
  generate_word_spot(func_double_double_t& cost_f = def_const) :
    cost_f_(cost_f)
    {}
};

それをクラスの静的メンバーにすることは理にかなっています

class generate_word_spot {
  ...
  static func_const_t def_const;
  ...
  generate_word_spot(func_double_double_t& cost_f = def_const) :
    cost_f_(cost_f)
    {}
};

func_const_t generate_word_spot::def_const(1);
于 2012-07-21T22:23:23.957 に答える