为什么不“外部模板"?使用shared_ptr? [英] Why won't "extern template" work with shared_ptr?

查看:196
本文介绍了为什么不“外部模板"?使用shared_ptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(看似)聪明的主意,就是在#include <memory>之后立即在stdafx.h中使用extern template class std::shared_ptr<SomeWidelyUsedClass>,以防止在数百个文件中冗余地实例化std::shared_ptr<SomeWidelyUsedClass>,因为我可以将template class std::shared_ptr<SomeWidelyUsedClass>放在一个文件中.单个.cpp,以便强制执行单个实例化,并希望节省编译/链接时间.但是,对生成的.cod和.obj文件的检查表明,无论如何,都正在创建shared_ptr<SomeWidelyUsedClass>代码.但是,如果我对自己的模板类使用完全相同的技术,则它可以按预期工作. shared_ptr有什么特殊之处可以阻止这种使用吗?也许<memory>本身会迫使编译器在到达我的extern template语句之前创建实例化(我敢肯定stdafx.h中没有使用shared_ptr的东西)?

I had the (seemingly) bright idea of using extern template class std::shared_ptr<SomeWidelyUsedClass> in stdafx.h immediately after #include <memory> in order to prevent std::shared_ptr<SomeWidelyUsedClass> from being redundantly instantiated in hundreds of files, figuring I could place template class std::shared_ptr<SomeWidelyUsedClass> in a single .cpp in order to force a single instantiation and hopefully save on compile/link time. However, examination of the resulting .cod and .obj files shows that shared_ptr<SomeWidelyUsedClass> code is being created everywhere anyway. But if I use this exact same technique with my own template class, it works as expected. Is there something special about shared_ptr that precludes this use? Perhaps something in <memory> itself that forces the compiler to create an instantiation before it reaches my extern template statement (I'm very certain there's nothing higher up in stdafx.h that makes use of shared_ptr)?

要澄清:

// stdafx.h; included in every cpp in the project
#include <memory>
#include "SomeWidelyUsedClass.h" // no shared_ptr in here

// I expect this to prevent instantiation of std::shared_ptr<SomeWidelyUsedClass>
// in all compilation units that include this, except the one below.
extern template class std::shared_ptr<SomeWidelyUsedClass>;

然后:

// ExplicitTemplateInstantiations.cpp
#include "stdafx.h"

// I expect this to cause std::shared_ptr<SomeWidelyUsedClass>
// to be instantiated in this compilation unit
template class std::shared_ptr<SomeWidelyUsedClass>;

并且:

// SomeOtherFile.cpp
#include "stdafx.h"
#include "SomeWidelyUsedClass.h"

void foo()
{
   // I expect that SomeOtherFile.obj will not include an instantiation of
   // std::shared_ptr<SomeWidelyUsedClass> since it was declared extern in stdafx.h
   std::shared_ptr<SomeWidelyUsedClass>(new SomeWidelyUsedClass());
}

推荐答案

该标准在§14.7.2/10中说:

The standard says in §14.7.2/10:

除内联函数和类模板专门化外,显式实例化声明具有 抑制它们所引用的实体的隐式实例化的效果.

Except for inline functions and class template specializations, explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer.

我刚刚在VS2013中进行了检查,std::shared_ptr<>的实现中有一个内联构造函数.这可能是您的extern template被忽略的原因.

I just checked in VS2013 and the implementation of std::shared_ptr<> there has an inline constructor. This is probably the reason why your extern template is ignored.

这篇关于为什么不“外部模板"?使用shared_ptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆