会员专业化不匹配任何模板声明
下面的代码编译除行之外,
template <> std::map<std::string, std::function<Goo*(Foo*)>>& Factory::getMap<Goo, Foo*>() {
return gooMap;
}
这里有什么问题,如何修复?编译器(GCC 11.2)指出,成员专业化与任何模板声明不匹配,但对我来说看起来确实如此。
#include <map>
#include <functional>
struct Foo; struct Goo;
struct Factory {
std::map<std::string, std::function<Foo*()>> fooMap;
std::map<std::string, std::function<Goo*(Foo*)>> gooMap;
template <typename T, typename U, typename... Args>
std::function<T*(Args&&...)> create() {
return [](Args&&... args) -> T* { return new U(std::forward<Args>(args)...); };
}
template <typename T, typename... Args>
std::map<std::string, std::function<T*(Args&&...)>>& getMap();
template <typename T, typename U, typename... Args>
void registerFunction (const std::string& name) {
getMap<T, Args...>()[name] = create<T, U, Args...>();
}
};
template <> std::map<std::string, std::function<Foo*()>>& Factory::getMap<Foo>() {
return fooMap;
}
template <> std::map<std::string, std::function<Goo*(Foo*)>>& Factory::getMap<Goo, Foo*>() {
return gooMap;
}
int main() {}
The code below compiles except for the lines
template <> std::map<std::string, std::function<Goo*(Foo*)>>& Factory::getMap<Goo, Foo*>() {
return gooMap;
}
What is wrong here, and how would it be fixed? The compiler (GCC 11.2) states that the member specialization does not match any template declaration, but to me it looks like it does.
#include <map>
#include <functional>
struct Foo; struct Goo;
struct Factory {
std::map<std::string, std::function<Foo*()>> fooMap;
std::map<std::string, std::function<Goo*(Foo*)>> gooMap;
template <typename T, typename U, typename... Args>
std::function<T*(Args&&...)> create() {
return [](Args&&... args) -> T* { return new U(std::forward<Args>(args)...); };
}
template <typename T, typename... Args>
std::map<std::string, std::function<T*(Args&&...)>>& getMap();
template <typename T, typename U, typename... Args>
void registerFunction (const std::string& name) {
getMap<T, Args...>()[name] = create<T, U, Args...>();
}
};
template <> std::map<std::string, std::function<Foo*()>>& Factory::getMap<Foo>() {
return fooMap;
}
template <> std::map<std::string, std::function<Goo*(Foo*)>>& Factory::getMap<Goo, Foo*>() {
return gooMap;
}
int main() {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论