मैं आदेश से प्रचुरता से फ़ाइलों के सैकड़ों में instantiated जा रहा है, मैं करने के लिए एक एकल के लिए मजबूर करने में एक भी सीपीपी में template class std::shared_ptr<SomeWidelyUsedClass>
लगा सके लगाना std::shared_ptr<SomeWidelyUsedClass>
को रोकने के लिए तुरंत #include <memory>
के बाद (प्रतीत होता है) stdafx.h में extern template class std::shared_ptr<SomeWidelyUsedClass>
का उपयोग कर के उज्जवल विचार था तत्कालता और आशा है कि संकलन/लिंक समय पर सहेजें। हालांकि, परिणामी .cod और .obj फ़ाइलों की जांच से पता चलता है कि shared_ptr<SomeWidelyUsedClass>
कोड हर जगह बनाया जा रहा है। लेकिन अगर मैं अपनी खुद की टेम्पलेट कक्षा के साथ इस सटीक तकनीक का उपयोग करता हूं, तो यह अपेक्षा के अनुसार काम करता है। क्या shared_ptr
के बारे में कुछ खास है जो इस उपयोग को रोकता है? शायद <memory>
में कुछ ऐसा है जो संकलक को extern template
कथन तक पहुंचने से पहले एक तात्कालिकता बनाने के लिए मजबूर करता है (मुझे यकीन है कि stdafx.h में कुछ भी ऊपर नहीं है जो shared_ptr
का उपयोग करता है)?share_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());
}
कुछ कोड होने से समस्या को समझने और निदान करने में मदद मिलेगी। –
डुनो, सवाल उतना उचित लगता है जितना यह है। हालांकि, जवाब नहीं पता। –
@RSahu कोड – dlf