eigen:模板函数中矩阵的默认类型
Eigen: Default type for MatrixBase in template function?
假设我有一些功能可以选择模板类型的可选参数。
template<typename Scalar = int>
void foo(Scalar *out = NULL)
{
std::cout << "HI!" << std::endl;
if(out != NULL)
*out = 42;
}
float x = 10;
foo(&x);//Call with optional argument
foo();//Call without optional argument
当然,如果没有可选参数,编译器就无法从呼叫中推导出可选的参数类型,但是我们可以通过指定默认模板参数来帮助它。
template<typename Scalar = int>
假设我现在有真实的示例eigen
template</*some template args*/, typename Derived>
void solve(/*some args*/, std::vector<Eigen::MatrixBase<Derived>> *variablePath)
我的问题是 - 如何为Derived
指定一些默认类型?例如,我想将变量路径的默认类型为 std::vector<Eigen::MatrixXf> *
当然我可以使用一些常见的模板参数,而不是Eigen::MatrixBase<Derived>
,例如
template</*some template args*/, typename Matrix = Eigen::MatrixXf>
void solve(/*some args*/, std::vector<Matrix> *variablePath)
,但我认为这很脏
ps对不起我的英语
我想您也想默认变量为nullptr
,因此只需编写一个没有可选参数的超载:
template</*some template args*/, typename MatType>
void solve(/*some args*/, std::vector<MatType> *variablePath);
template</*some template args*/>
void solve(/*some args*/) {
std::vector<MatrixXf> *nullvector = nullptr;
solve(/*some args*/, nullvector);
}
相关文章:
- 具有默认模板类型的默认构造函数的类型推导
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 模板类默认类型和条件
- 具有默认类型和值的模板参数
- 在一行中将默认类型值上的指针作为参数传递
- 模板中默认类型的重载运算符
- 当“std::make_index_sequence”和“std::index_sequence”用于模板参数默认类型时
- 所有版本的 GCC 都在与定义中具有默认类型的模板作斗争
- 有关为模板提供默认类型的问题
- eigen:模板函数中矩阵的默认类型
- 可以将枚举的默认类型设置为无符号字符
- 是否可以为缺少的模板成员类型创建默认类型?
- 如何在C++中为类返回默认类型
- 函数模板:默认类型参数
- 模板默认类型vs默认值
- 使用默认类型替换c++函数引用模板错误
- 模板函数的默认类型假设
- 在C++中,用十六进制或八进制表示的整型文字的默认类型是什么
- 如何在c++中定义默认类型转换操作符
- 错误:不明确的默认类型转换(c++)