jeudi 22 avril 2021

How to prevent the implicit instantiation of std::string/std::wstring in static library

I have written a sample static library.
I want to suppress the implicit instantiation of std::string/std::wstring. I referred this link and I declared the explicit instantiation of template class std::basic_string. I expected that the static lib wont contain any symbols for std::basic_string. But the dumpbin utility showed there are symbols present for basic_string in the library. The code is as follows:

// testLib.h   
#pragma once

namespace staticLib{

    void TestLib();
}

// testLib.cpp  
#include < string >     
#include "testLib.h";

//Explicit instantiation declaration   
extern template class std::basic_string< char> ; // Is this correct?

namespace staticLib {

    void TestLib() {
        std::string str = "Testing";
        str.size();
        str.erase();
    }
}

The dumpbin log : enter image description here

My questions are as follows:

  1. Is there any way to suppress the implicit instantiation of std::string/std::wstring.
  2. There is an 'explicit instantiation declaration for std::basic_string' in testLib.cpp. Is that correct ?
  3. Why the declaration of explicit instantiation of basic_string does not prevent the implicit instantiation and the symbols in the library.

Aucun commentaire:

Enregistrer un commentaire