Resolving compilation error with template specialization

I am having issues in understanding and fixing a compiler error that I see with MinGW (UCRT64) C++20, but that I don’t see when I build the code with MSBuild C++11.

header.h: (Only relevant code)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#pragma once
// Has other necessary inclusions
#include <string>
#include <map>
struct Foo {
// Assumptions:
// Has some members
// Has variants of constructors
};
using Fooes = std::multimap<size_t, Foo>;
template <typename S>
std::string retrieveData(S& params, const std::string& tag, const std::string& initval);
extern template bool retrieveData<const Fooes>(const Fooes& params, const std::string& tag, bool initval);
// There are similar such retrieveData `extern template`s which deals with different data types for args and returning
// I have commented them out with the thought that I'll fix one and deal with the remaining later
</code>
<code>#pragma once // Has other necessary inclusions #include <string> #include <map> struct Foo { // Assumptions: // Has some members // Has variants of constructors }; using Fooes = std::multimap<size_t, Foo>; template <typename S> std::string retrieveData(S& params, const std::string& tag, const std::string& initval); extern template bool retrieveData<const Fooes>(const Fooes& params, const std::string& tag, bool initval); // There are similar such retrieveData `extern template`s which deals with different data types for args and returning // I have commented them out with the thought that I'll fix one and deal with the remaining later </code>
#pragma once
// Has other necessary inclusions
#include <string>
#include <map>

struct Foo {
    // Assumptions:
    // Has some members
    // Has variants of constructors
};

using Fooes = std::multimap<size_t, Foo>;

template <typename S>
std::string retrieveData(S& params, const std::string& tag, const std::string& initval);

extern template bool retrieveData<const Fooes>(const Fooes& params, const std::string& tag, bool initval);

// There are similar such retrieveData `extern template`s which deals with different data types for args and returning
// I have commented them out with the thought that I'll fix one and deal with the remaining later

Source.cpp: (Only relevant code)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Safe to assume that:
// Has other necessary inclusions
#include <header.h>
Fooes::const_iterator retrieveEntry(const Fooes& params, const std::string& tag) {
// Impl that is already functional, has no warnings
}
template<>
std::string retrieveData<const TaxParameters>(const TaxParameters& params, const std::string& tag, const std::string& initval) {
const auto matchedEntry = retrieveEntry(params, tag);
if (matchedEntry == params.cend()) {
return initval;
}
return matchedEntry->second.value;
}
// There are similar such retrieveData impl which deals with different data types for args and returning
// I have commented them out with the thought that I'll fix one and deal with the remaining later
</code>
<code>// Safe to assume that: // Has other necessary inclusions #include <header.h> Fooes::const_iterator retrieveEntry(const Fooes& params, const std::string& tag) { // Impl that is already functional, has no warnings } template<> std::string retrieveData<const TaxParameters>(const TaxParameters& params, const std::string& tag, const std::string& initval) { const auto matchedEntry = retrieveEntry(params, tag); if (matchedEntry == params.cend()) { return initval; } return matchedEntry->second.value; } // There are similar such retrieveData impl which deals with different data types for args and returning // I have commented them out with the thought that I'll fix one and deal with the remaining later </code>
// Safe to assume that:
// Has other necessary inclusions
#include <header.h>

Fooes::const_iterator retrieveEntry(const Fooes& params, const std::string& tag) {
    // Impl that is already functional, has no warnings
    }

template<>
std::string retrieveData<const TaxParameters>(const TaxParameters& params, const std::string& tag, const std::string& initval) {
    const auto matchedEntry = retrieveEntry(params, tag);
    if (matchedEntry == params.cend()) {
        return initval;
    }
    return matchedEntry->second.value;
}

// There are similar such retrieveData impl which deals with different data types for args and returning
// I have commented them out with the thought that I'll fix one and deal with the remaining later

Compilation Error that I get:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>error: specialization of 'std::string retrieveData(S&, const std::string&, const std::string&) [with S = const std::multimap<long long unsigned int, Foo>; std::string = std::__cxx11::basic_string<char>]' after instantiation
std::string retrieveData<const Fooes>(const Fooes& params, const std::string& tag, const std::string& initval)
</code>
<code>error: specialization of 'std::string retrieveData(S&, const std::string&, const std::string&) [with S = const std::multimap<long long unsigned int, Foo>; std::string = std::__cxx11::basic_string<char>]' after instantiation std::string retrieveData<const Fooes>(const Fooes& params, const std::string& tag, const std::string& initval) </code>
error: specialization of 'std::string retrieveData(S&, const std::string&, const std::string&) [with S = const std::multimap<long long unsigned int, Foo>; std::string = std::__cxx11::basic_string<char>]' after instantiation
std::string retrieveData<const Fooes>(const Fooes& params, const std::string& tag, const std::string& initval) 

I looked at the CPP reference to understand the Explicit instantiations as I have never worked with those. It talks about usage of extern template in following way:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>extern template return-type name < argument-list > ( parameter-list ) ; (3) (since C++11)
extern template return-type name ( parameter-list ) ; (4) (since C++11)
</code>
<code>extern template return-type name < argument-list > ( parameter-list ) ; (3) (since C++11) extern template return-type name ( parameter-list ) ; (4) (since C++11) </code>
extern template return-type name < argument-list > ( parameter-list ) ; (3) (since C++11)
extern template return-type name ( parameter-list ) ;   (4) (since C++11)

And,

An explicit instantiation definition forces instantiation of the function or member function they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program, no diagnostic required.

An explicit instantiation declaration (an extern template) prevents implicit instantiations: the code that would otherwise cause an implicit instantiation has to use the explicit instantiation definition provided somewhere else in the program.

I am so confused to figure out in my code which one is explicit instantiation definition and which one is explicit instantiation declaration, And do I refer to the implementation of the function in source.cpp as Specialization as the compilation error says.

What is the correct way to fix this error?

6

in header.h, the return type of specialization should be

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>extern template std::string
retrieveData<const Fooes>(const Fooes& params, const std::string& tag, bool initval);
</code>
<code>extern template std::string retrieveData<const Fooes>(const Fooes& params, const std::string& tag, bool initval); </code>
extern template std::string
retrieveData<const Fooes>(const Fooes& params, const std::string& tag, bool initval);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật