默认参数值中的static_cast
static_cast in default argument value
我想要一个默认参数为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){ ... };
}
并且CCD_ 1是许多类似于此的函数对象的基类。
GCC对上面的构造函数说"无效的static_cast
"。有没有办法实现这样的行为?
您确定在您的案例中需要一个非常数引用吗?如果你可以使用常量引用,那么只需进行
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);
相关文章:
- 如何理解C++标准N3337中的expr.const.cast子句8
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- C++Cast运算符过载
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- LNK1104:无法打开libpjproject-i386-Win32-vc14-Debug-Static.lib
- 我应该在 C++ 中何时/为什么使用 STATIC?
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- 如何处理Boost Spirit X3导致Visual Studio 2019 "static initialization order fiasco"?
- "static char __ = []() -> char"的含义
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 无法在 DLL 中链接 SDL2-static.lib
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 错误:"cast"未命名类型void setCastDescription(std::string
- 通过使用 const-cast 的非常量引用来延长临时的寿命
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- static是如何使用ClassA::m_variable处理所有类对象的
- 在[expr.static.cast]/4中,术语"一个可行函数"指的是什么