gpt4 book ai didi

c++ - 即使传入也无法确定模板类型

转载 作者:行者123 更新时间:2023-11-27 22:58:10 25 4
gpt4 key购买 nike

<分区>

我有以下代码:

template <typename T>
struct Data {
struct Embed
{
T t;
};
};

struct Functor {
template <typename T>
void foo( typename Data<T>::Embed & e) {}
};
template <typename T, typename F>
struct Caller
{
F f;
template <typename T>
void invoke() {
typename Data<T>::Embed e;
f.foo<T>(e); //compiler error pointed this line
}
};

然后我将模板专门化为:

 Caller<int, Functor> c;
c.invoke();

编译器错误是:error: expected primary-expression before '>'f.foo<T>(e);线。似乎编译器突然不知道 T 是什么,即使它是在函数的模板声明中指定的。

取出明确指定的Tfoo.invoke(e)行将结果 could not deduce template parameter 'T'

我该如何解决这个问题? (我仍然想保留 Caller 可以具有通用仿函数并且可以模板化仿函数的功能)。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com