有时我会尝试自学泛型类和函数声明以及 Boost 库。
我遇到过一个例子,不太明白什么是typename f=...
方法。
你能帮我理解使用 =
模板声明的概念吗?在以下情况下登录:template <typename T, typename F=ublas::row_major>
?这是我试图理解的完整程序。
#include <algorithm>
#include <vector>
#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
namespace ublas = boost::numeric::ublas;
template <typename T, typename F=ublas::row_major>
ublas::matrix<T, F> makeMatrix(std::size_t m, std::size_t n, const std::vector<T> & v)
{
if(m*n!=v.size()) {
; // Handle this case
}
ublas::unbounded_array<T> storage(m*n);
std::copy(v.begin(), v.end(), storage.begin());
return ublas::matrix<T>(m, n, storage);
}
最佳答案
您传递给模板的是默认参数。 例如
Template<typename T, int N = 17>
class Generic
{
}
这里,第二个参数是默认值
在你的情况下,F=ublas::row_major 是默认值。
深入了解http://en.cppreference.com/w/cpp/language/template_parameters
关于c++ - 当c++模板定义带有=时是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27224529/