gpt4 book ai didi

C++ 模板 typedef 作为模板函数参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:20 24 4
gpt4 key购买 nike

为什么编译器找不到 read1 的匹配项?我看不出 read1read2 之间的区别;嵌套的 typedef 模板是否有限制,例如 Foo 类中的模板?

template<typename T>
class Handle{};

class Foo{
public:
typedef Handle<Foo> Handle;
};


template<typename T>
void read1(typename T::Handle){}

template<typename T>
void read2(Handle<T>){}


int main(int argc, char** argv)
{
Foo::Handle f1;
read1(f1);

Foo::Handle f2;
read2(f2);
}

G++ 编译器输出,(G++ 4.4.5)

g++ -c -I.  main1.cpp 
main1.cpp: In function ‘int main(int, char**)’:
main1.cpp:37: error: no matching function for call to ‘read1(Handle<Foo>&)’

最佳答案

Foo::Handle f1;    
read1(f1);

传递给 read1 的类型是 Handle<Foo> ,不是福。

模板不是继承。 Handle<Foo>是一个不同的类,不是 Foo,所以没有 Handle<Foo>::Handle .

关于C++ 模板 typedef 作为模板函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8611206/

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