0

I would like to have a constructor with default argument static_cast like:

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

where

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

and func_double_double_t is the base class for many function objects similar to this.

GCC says "invalid static_cast" for the above constructor. Is there a way to achieve such a behavior?

Sisir
  • 4,584
  • 4
  • 26
  • 37
Emre Sahin
  • 468
  • 4
  • 14
  • Looks like you're trying to bind a temporary to a non-const reference. – chris Jul 21 '12 at 22:19
  • Are you actually using `substitution_cost` parameter at all? Or you just omitted some code? – betabandido Jul 21 '12 at 22:22
  • sorry, i deleted other arguments and changed some names. correcting. – Emre Sahin Jul 21 '12 at 22:25
  • Where did `static_cast` come from? You are converting from derived class to base class. Such conversion requires no cast at all. Why are you insisting on `static_cast` and where did it even come from? – AnT stands with Russia Jul 21 '12 at 22:26
  • @AndreyT why does it say `error: default argument for parameter of type ‘otap::func_double_double_t&’ has type ‘otap::func_const_t’` when I try `generate_word_spot(func_double_double_t& cost_f = func_const_t(1))` as a default argument? – Emre Sahin Jul 21 '12 at 22:30
  • BTW `func_double_double_t` is abstract. – Emre Sahin Jul 21 '12 at 22:32
  • @Emre Sahin: The problem is that in C++ you cannot attach a *non-const* reference to a *temporary* object. The compiler simply gives you an imprecise error message. `static_cast` won't save you here and has no business being here. Either switch to const reference (if you can) or do what I said in my answer. – AnT stands with Russia Jul 21 '12 at 22:32
  • Umm, right. I put `static_cast` when I was trying to understand GCC. Will put a few `const`s here and there. I'm accepting your answer below. Thank you. – Emre Sahin Jul 21 '12 at 22:36

1 Answers1

1

Are you sure you need a non-const reference in your case? If you can use a const reference then just do

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

No cast necessary. (Neither is the ; after the definition.)

Otherwise, for non-const reference binding temporary object is out of question. You'd need to declare a standalone non-temporary object to use as default argument

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)
    {}
};

It makes sense to make it static member of the class

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);
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765