【发布时间】:2017-11-22 15:14:34
【问题描述】:
使用下面的代码,我遇到了编译器错误:
type/value mismatch at argument 1 in template parameter list for 'template<class _Tp> class std::shared_ptr'
std::shared_ptr<T> shared_pointer;
^
../test/main.cpp:16:22: note: expected a type, got 'T'
不明白为什么T 没有正确推断为shared_pointer?
template<typename K>
class Bar{
K k;
};
template<template<typename> class T>
class Foo{
public:
std::shared_ptr<T> shared_pointer;
};
int main(void)
{
Foo<Bar<std::string>> foo;
}
更新: 另一个例子:
template<typename TObject, template<TObject> class TBuffer>
class BaseGrabber {
public:
virtual void run(std::shared_ptr<TBuffer>){};
};
当有人写类似的东西时,我想强制编译器出错
BaseGrabber<int, Bar<long>> grabber;
所以Bar 从来没有专门使用不同于第一个BaseGrabber 模板参数的类型。
【问题讨论】:
-
template<typename>在template<template<typename> class T>中的作用是什么? -
错误消息已损坏:这是预期类型与
T的 模板 之间的不匹配。 IOW,std::shared_ptr<T<something>>会工作。 -
@AlgirdasPreidžius 的意思是(我想)一旦您删除
template<typename>并将 Foo` 定义为template<class T>,一切都会正常工作。 -
我正在使用多个模板参数,当有人尝试使用不同模板类型实例化模板类时,我想强制编译器出错。更新的问题。
-
@AlanKazbekov 然后你可以将它用作
BaseGrabber<int, Bar>,并使用模板参数TObject实例化BaseGrabber中的模板。
标签: c++ templates type-deduction