特征:模板函数中 MatrixBase 的默认类型?
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
指定一些默认类型?
例如,我想将 variablePath 的默认类型设置为 std::vector<Eigen::MatrixXf> *
当然我可以使用一些通用的模板参数来代替 Eigen::MatrixBase<Derived>
例如
template</*some template args*/, typename Matrix = Eigen::MatrixXf>
void solve(/*some args*/, std::vector<Matrix> *variablePath)
但是我觉得很脏
PS对不起我的英语
我猜你也想默认 variablePath 到 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);
}
假设我有一些函数可以接受模板类型的可选参数。
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
指定一些默认类型?
例如,我想将 variablePath 的默认类型设置为 std::vector<Eigen::MatrixXf> *
当然我可以使用一些通用的模板参数来代替 Eigen::MatrixBase<Derived>
例如
template</*some template args*/, typename Matrix = Eigen::MatrixXf>
void solve(/*some args*/, std::vector<Matrix> *variablePath)
但是我觉得很脏
PS对不起我的英语
我猜你也想默认 variablePath 到 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);
}