简体   繁体   English

在Visual Studio 2015解决方案中实现C ++模板的正确方法是什么?

[英]What is the correct way to implement a C++ template in a Visual Studio 2015 solution?

I have an assignment in my computer science class where we need to create a templatized version of a C++ class. 我的计算机科学课上有一个作业,我们需要创建C ++类的模板化版本。 Our instructor has given us little to no instruction regarding the matter so I've been fumbling through the assignment. 我们的讲师很少给我们关于这件事的指导,所以我一直在摸索作业。

I seem to have made the code from our book work, but I'm not sure the approach I used is appropriate. 我似乎已经从书本上编写了代码,但是我不确定我使用的方法是否合适。

Essentially, the example they provide in the book is a "bag" class which is a simple container used to store any manner of standard data types (int, char, string, etc.). 本质上,他们在书中提供的示例是“ bag”类,它是一个用于存储任何形式的标准数据类型(int,char,string等)的简单容器。 The templatized version of this class is declared in a header file called bag5.h and the implementation is given in a .template file called bag5.template. 此类的模板化版本在名为bag5.h的头文件中声明,而实现在名为bag5.template的.template文件中给出。 The header then retrieves the code from the .template file using this line, which is near the very end of the header: 然后,标头使用以下行从.template文件中检索代码,该行位于标头的最末端:

#include "bag5.template" . #include "bag5.template"

At first I wasn't sure how to implement a file of type .template in a C++ solution using Visual Studio 2015. I searched for an answer but unfortunately, searching for the terms "visual studio" and "template" provide nearly limitless results on how to create templates for visual studio projects, but very little I could find on properly implementing generic C++ templates in Visual Studio. 最初,我不确定如何使用Visual Studio 2015在C ++解决方案中实现.template类型的文件。我搜索了一个答案,但不幸的是,搜索“ Visual Studio”和“ template”这两个术语在如何为Visual Studio项目创建模板,但是在Visual Studio中正确实现通用C ++模板方面我几乎找不到。

Through trial and error, I finally got the source code to work by adding the .template files to the solution as a file of type "text" rather than as a C++ source file. 经过反复试验,最终我通过将.template文件添加为“文本”类型的文件而不是C ++源文件的方法来使源代码正常工作。 My question is simple: Is this the correct way to implement the definition for a declared template using Visual Studio 2015? 我的问题很简单:这是使用Visual Studio 2015为声明的模板实现定义的正确方法吗?

Also, is there any way to get Visual Studio 2015 to recognize my .template files as C++ code and format them accordingly? 此外,是否有任何方法可以使Visual Studio 2015将我的.template文件识别为C ++代码并进行相应的格式设置? When I add the .template implementation files as type "text" within the solution, the IDE does not color code the text inside the files, it leaves it completely black on white text. 当我在解决方案中将.template实现文件添加为“文本”类型时,IDE不会对文件中的文本进行颜色编码,而是将其完全留在白色文本上。

The usual way to do this is to put the code for the template directly in the header. 通常的方法是将模板代码直接放在标题中。

If you really want to keep the code separate from the declarations, you can choose two different extensions that VC++ recognizes as headers, and have the one include the other. 如果您确实想使代码与声明分开,则可以选择VC ++识别为标头的两个不同扩展,并让其中一个包含另一个。 For example, have bag5.h , which includes bag5.hh . 例如,拥有bag5.h ,其中包括bag5.hh

As long as both extensions are recognized by VC++ as headers, life is pretty good--you don't have to do much to tell it how to work with them, and it'll automatically apply color coding to both. 只要VC ++都将这两个扩展名识别为标头,那么生活就相当不错了-您不必花太多时间告诉它如何使用它们,并且它将自动对这两个应用颜色编码。

There's nothing wrong with it - you can name your files whatever you'd like. 它没有任何问题-您可以根据需要命名文件。

But it's pretty unconventional; 但这非常不合常规。 I've never seen the extension .template used, and apparently neither has your IDE. 我从未见过使用扩展名.template ,并且显然也没有IDE。

I would use .ipp , or just .h . 我会用.ipp ,或者只是.h You could see whether your IDE prefers these extensions. 您可以看到您的IDE是否更喜欢这些扩展。

If you are required to use .template , then you have done so correctly thus far. 如果需要使用.template ,那么到目前为止您已经正确地使用了。 I don't know whether you can force VS to highlight such files as C++. 我不知道您是否可以强制VS突出显示C ++这样的文件。

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

相关问题 在 Visual Studio 中编译 C++ 的正确方法? - correct way to compile c++ in visual studio? 在Visual Studio 2015中找不到C ++的模板 - Can't find template for C++ in Visual Studio 2015 如何在Visual Studio 2015中成功导出C ++项目模板? - How to successfully export C++ project template in Visual Studio 2015? 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖源代码控制的最佳方法是什么? - What is the best way to handle source control of an external dependency for a C++ Visual Studio 2019 Solution using Git? 在 Visual Studio 中为 C++ 设置 UnitTest 项目并在主项目中包含其他目录的正确方法是什么? - What is the correct way to set up a UnitTest project for C++ in Visual Studio with additional include directories in the main Project? Visual Studio 2015 C ++解决方案无法在系统PATH中看到svn exe - Visual Studio 2015 C++ solution can't see svn exe in system PATH 在构建 C++ 项目时,Visual Studio CE 2019 和 Visual Studio CE 2015 之间有什么区别? - What differences are there between Visual Studio CE 2019 and Visual Studio CE 2015 when building C++ projects? Visual Studio 2015 No C ++ CLR模板 - Visual Studio 2015 No C++ CLR Templates Visual Studio 2015 C ++的助手 - Helper for visual studio 2015 for C++ Visual Studio 2015中没有C ++项目属性 - No C++ Project Properties in Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM