为模板使用别名 class
Using alias for template class
我没有找到关于这个问题的任何答案:
我正在写一个模板 class DenseMatrix ,在 Matrix 之前使用规范 "Dense" 的必要性是因为这个 class 在一个层次结构中,在顶部有基本抽象 class 从中导出 SparseMatrix 的矩阵(从中导出很多 class 表示稀疏矩阵的不同存储方式,如 CRS 、 MCRS 、 BlockCSR ..)
现在我想在 main 函数中提供例如使用简单名称矩阵实例化 class DenseMatrix 的 object 的可能性(注意这不是像抽象基础 class 那样的矩阵),所以我记得在 C 中可以使用
typdef struct {
} name ;
然后
name obj ; // instance of struct
我想获得相同的东西,但在 C++ 中 class(面向 C++11)
这是直接在 DenseMatrix class 声明的 header 中执行此操作的最佳方法?
P.S。在定义方法期间,我总是使用 DenseMatrix:: 而不是别名
编辑这里的例子,看代码的结尾
# include "Matrix.H"
template <typename Type>
class DenseMatrix ;
template<typename U>
std::ostream& operator<<(std::ostream& os, const DenseMatrix<U>& m )
template <typename Type>
class DenseMatrix
: public Matrix<Type>
{
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const DenseMatrix<U>& m );
//--
//
public:
constexpr DenseMatrix (std::initializer_list<std::vector<Type>> ) noexcept ;
constexpr DenseMatrix (const std::string& );
// constexpr DenseMatrix (std::size_t , std::size_t);
virtual ~DenseMatrix() = default ;
Type& operator()(const std::size_t , const std::size_t) noexcept override;
const Type& operator()(const std::size_t , const std::size_t ) const noexcept override;
void constexpr print () const noexcept override ;
auto constexpr size1()const noexcept { return Rows ; }
auto constexpr size2()const noexcept { return Cols ; }
Type constexpr findValue(const std::size_t , const std::size_t ) const noexcept ;
protected:
std::vector<Type> data ;
std::size_t Rows ;
std::size_t Cols ;
mutable Type dummy ;
} ;
// here ------\/ -------
template <typename T>
using matrix<T> = DenseMatrix<T>
感谢@R2RT,这正是我要找的! 已解决
如果我猜对了,那么你就快到了,但是你 <T>
太多了:
template <typename T>
using matrix = DenseMatrix<T>;
这叫做模板别名
An alias template is a template which, when specialized, is equivalent to the result of substituting the template arguments of the alias template for the template parameters in the type-id
我没有找到关于这个问题的任何答案: 我正在写一个模板 class DenseMatrix ,在 Matrix 之前使用规范 "Dense" 的必要性是因为这个 class 在一个层次结构中,在顶部有基本抽象 class 从中导出 SparseMatrix 的矩阵(从中导出很多 class 表示稀疏矩阵的不同存储方式,如 CRS 、 MCRS 、 BlockCSR ..) 现在我想在 main 函数中提供例如使用简单名称矩阵实例化 class DenseMatrix 的 object 的可能性(注意这不是像抽象基础 class 那样的矩阵),所以我记得在 C 中可以使用
typdef struct {
} name ;
然后
name obj ; // instance of struct
我想获得相同的东西,但在 C++ 中 class(面向 C++11) 这是直接在 DenseMatrix class 声明的 header 中执行此操作的最佳方法? P.S。在定义方法期间,我总是使用 DenseMatrix:: 而不是别名
编辑这里的例子,看代码的结尾
# include "Matrix.H"
template <typename Type>
class DenseMatrix ;
template<typename U>
std::ostream& operator<<(std::ostream& os, const DenseMatrix<U>& m )
template <typename Type>
class DenseMatrix
: public Matrix<Type>
{
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const DenseMatrix<U>& m );
//--
//
public:
constexpr DenseMatrix (std::initializer_list<std::vector<Type>> ) noexcept ;
constexpr DenseMatrix (const std::string& );
// constexpr DenseMatrix (std::size_t , std::size_t);
virtual ~DenseMatrix() = default ;
Type& operator()(const std::size_t , const std::size_t) noexcept override;
const Type& operator()(const std::size_t , const std::size_t ) const noexcept override;
void constexpr print () const noexcept override ;
auto constexpr size1()const noexcept { return Rows ; }
auto constexpr size2()const noexcept { return Cols ; }
Type constexpr findValue(const std::size_t , const std::size_t ) const noexcept ;
protected:
std::vector<Type> data ;
std::size_t Rows ;
std::size_t Cols ;
mutable Type dummy ;
} ;
// here ------\/ -------
template <typename T>
using matrix<T> = DenseMatrix<T>
感谢@R2RT,这正是我要找的! 已解决
如果我猜对了,那么你就快到了,但是你 <T>
太多了:
template <typename T>
using matrix = DenseMatrix<T>;
这叫做模板别名
An alias template is a template which, when specialized, is equivalent to the result of substituting the template arguments of the alias template for the template parameters in the type-id