header.h: (только релевантный код)
Код: Выделить всё
#pragma once
// Has other necessary inclusions
#include
#include
struct Foo {
// Assumptions:
// Has some members
// Has variants of constructors
};
using Fooes = std::multimap;
template
std::string retrieveData(S& params, const std::string& tag, const std::string& initval);
extern template bool retrieveData(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
Код: Выделить всё
// Safe to assume that:
// Has other necessary inclusions
#include
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& 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
Код: Выделить всё
error: specialization of 'std::string retrieveData(S&, const std::string&, const std::string&) [with S = const std::multimap; std::string = std::__cxx11::basic_string]' after instantiation
std::string retrieveData(const Fooes& params, const std::string& tag, const std::string& initval)
Код: Выделить всё
extern template return-type name < argument-list > ( parameter-list ) ; (3) (since C++11)
extern template return-type name ( parameter-list ) ; (4) (since C++11)
Явное определение создания экземпляра приводит к созданию экземпляра функции или функции-члена, на которую они ссылаются. Он может появиться в программе где угодно после определения шаблона, и для данного списка аргументов разрешено появляться в программе только один раз, диагностика не требуется.
Явное объявление экземпляра (шаблон extern) предотвращает неявное создание экземпляров: код, который в противном случае вызвал бы неявное создание экземпляров, должен использовать определение явного создания экземпляра, предоставленное где-то в другом месте программы.
Я так смущен, пытаясь выяснить в своем коде, какое из них является явным определением экземпляра, а какое - явным объявлением экземпляра. И называю ли я реализацию функции в source.cpp специализацией, как говорит ошибка компиляции.
Как правильно исправить эту ошибку?
Подробнее здесь: https://stackoverflow.com/questions/789 ... ialization