简体   繁体   English

使用typedef表示unique_ptr模板

[英]using typedef for unique_ptr template

The following code compiles. 以下代码编译。

matrix.h before template 在模板之前的matrix.h

template<typename T>
class Matrix
{
public:
    //...
    unique_ptr<Matrix<T>> Test() const;
};

matrix.cpp before template 在模板之前的matrix.cpp

template<typename T>
unique_ptr<Matrix<T>> Matrix<T>::Test() const
{
    unique_ptr<Matrix<T>> a{ new Matrix<T>{ 1, 1 } };
    return std::move(a);
}

I wanted to use a typedef (using) to shorten things as I thought it'd be more readable but my changes cause errors. 我想使用typedef(使用)来缩短内容,因为我认为它更具可读性,但我的更改会导致错误。 Here are the relevant changes. 以下是相关更改。

matrix.h after template matrix.h在模板之后

template<typename T>
class Matrix
{
public:
    //...
    MatrixUniq<T> Test() const;
};

template<class T> using MatrixUniq = unique_ptr<Matrix<T>>;

matrix.cpp after template matrix.cpp在模板之后

template<typename T>
MatrixUniq<T> Matrix<T>::Test() const
{
    MatrixUniq<T> a{ new Matrix<T>{ 1, 1 } };
    return std::move(a);
}

Compiling after these changes are made crashes the VC++ compiler twice, but also generates a few errors: 进行这些更改后进行编译会使VC ++编译器崩溃两次,但也会产生一些错误:

Error   C2143   syntax error: missing ';' before '<'    
Error   C4430   missing type specifier - int assumed. 
Error   C2238   unexpected token(s) preceding ';'
Error   C1903   unable to recover from previous error(s);

What's wrong with my typedef implementation? 我的typedef实现有什么问题? Thanks. 谢谢。

Edit: I'm using VS2015. 编辑:我正在使用VS2015。 I'm building a static library. 我正在构建一个静态库。 At the bottom of matrix.cpp I have: 在matrix.cpp的底部,我有:

template class VMatrix<double>;

You are using the MatrixUniq<T> alias before having defined it. 您在定义之前使用MatrixUniq<T>别名。

Move the using inside the class: 在课堂内移动using

template<typename T>
class Matrix
{
public:
    template<class U> using MatrixUniq = std::unique_ptr<Matrix<U>>;

    MatrixUniq<T> Test() const;
};

And change the definition accordingly: 并相应地更改定义:

template<typename T>
Matrix<T>::MatrixUniq<T> Matrix<T>::Test() const
{
    return MatrixUniq<T>{ new Matrix<T>{ 1, 1 } };
}

Or if you want to have it in the global namespace, define it before the class definition after a forward declaration on the class: 或者,如果要在全局命名空间中使用它,请在类的前向声明之后在类定义之前定义它:

template<typename T>
class Matrix;

template<class T> using MatrixUniq = std::unique_ptr<Matrix<T>>;

template<typename T>
class Matrix
{
public:
    //...
    MatrixUniq<T> Test() const;
};

Also you don't need to explicitly do a std::move when returning local variables. 此外,在返回局部变量时,您不需要显式执行std::move Local variables that are returned are automatically moved by default. 默认情况下,返回的局部变量会自动移动。

try this: 尝试这个:

template<typename T>
class Matrix
{
public:
    using unique_ptr_type = std::unique_ptr<Matrix>;
    //...
    unique_ptr_type Test() const;
};

template<class T> using MatrixUniq = typename Matrix<T>::unique_ptr_type;

template<typename T>
typename Matrix<T>::unique_ptr_type Matrix<T>::Test() const
{
    return unique_ptr_type(new Matrix());
}

Always make sure to declare templates above the code which will be using it. 始终确保在将使用它的代码上方声明模板。

Also this snippet: 还有这个片段:

template<class T> using MatrixUniq = unique_ptr<Matrix<T>>;

might not be a correct implementation.

Here's how you can declare a type definition in c++.

typedef <type> <var_name>

Here's another example using an 'Alias Template' 这是使用“别名模板”的另一个例子

template<typename T>
using MyVector = std::vector<T, MyCustomAllocator<T>>;

The rest of the code is for you to debug. 其余代码供您调试。

See this relevant discussion here: 请参阅此处的相关讨论:

How to typedef a template class? 如何输入定义模板类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM