c++ - 为什么 gcc 告诉我我的模板不是模板?
问题描述
我有这样一段代码:
template<template<typename> class ContainerType,
typename ValueType,
typename ReturnType>
struct value_extractor
{
public:
static ReturnType extract(const ContainerType<ValueType>&);
};
template<template<typename> class ContainerType,
typename ValueType>
struct value_extractor<ContainerType, ValueType, std::shared_ptr<ValueType>>
{
static std::shared_ptr<ValueType> extract(const ContainerType<ValueType>& value)
{
return value;
}
};
它本质上是从模板类型中提取值。这段代码使用 clang 编译得很好,但是使用 gcc 我得到一个错误说:
g++ test.cpp -lstdc++ -O2
In file included from di.hpp:1:0,
from test.cpp:2:
holders.hpp: In instantiation of ‘ReturnType di::holder<ContainerType, ValueType>::provide() const [with ReturnType = std::shared_ptr<int>; ContainerType = std::shared_ptr; ValueType = int]’:
di.hpp:35:105: required from ‘static ReturnType di::holder_selector::convert(const types_map&, ContainerType<ValueType>*) [with ReturnType = std::shared_ptr<int>; ContainerType = std::shared_ptr; ValueType = int; di::types_map = std::unordered_map<void (*)(), std::unique_ptr<di::base_holder> >]’
di.hpp:40:39: required from ‘T di::injector::provide() const [with T = std::shared_ptr<int>]’
test.cpp:14:63: required from here
holders.hpp:48:85: error: ‘di::value_extractor<ContainerType, ValueType, std::shared_ptr<_Up> >::extract(const ContainerType<ValueType>&) [with ContainerType = std::shared_ptr; ValueType = int]’ is not a template [-fpermissive]
alue_extractor<ContainerType, ValueType, ReturnType>::template extract(value_);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
如果我使用该-fpermissive
标志,则代码可以编译甚至可以工作,但显然会发出警告。所以我的问题是:真的是我还是这是 gcc 的错误,如果是我在编写不合格的代码,那么我应该如何解决它?提前致谢。
解决方案
调用提取时不需要模板,愚蠢的错误。感谢@songyuanyao
推荐阅读
- php - Row Count 输出 - 在表中的行数前面
- regex - 如何替换与 egrep 匹配的较大字符串中的子字符串?
- python - Foursquare API 在 Python 中返回零 checkinsCount 签入计数,但在在线试用界面中返回非零值
- android - 向 cordova.exec 函数添加额外的参数
- php - PHP - 访问对象属性 $id 时出错
- css - 打印时,Vuetify 应用程序为不可见元素保留空间
- c++ - 带有 tinyobjloader 的 OpenGL 顶点数组对象
- python - 通过 Flask-SocketIO-Kafka 将服务器生成的消息发送回客户端
- python - Spark 错误地读取 CSV
- python - Pytorch:.grad() 函数如何返回结果?