gpt4 book ai didi

c++ - 函数局部类拒绝模板实例化

转载 作者:太空狗 更新时间:2023-10-29 21:08:18 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How to use local classes with templates?

g++ 4.4 拒绝编译对以函数局部类作为模板参数的模板函数的调用。像这样:

// Given this:
template <typename C>
int f(const C& c) {
return c.g();
}

// This compiles fine:
struct C1 {
int g() const { return 42; }
};

int h1() {
return f(C1());
}

// But this doesn't:
int h2() {
struct C2 {
int g() const { return 42; }
};
return f(C2()); // error: no matching function for call to "f(h2()::C2)"
}

// Nor does this:
int h3() {
struct C3 {
int g() const { return 42; }
};
return f<C3>(C3()); // same error
}

什么给了?我如何使这项工作? (在修剪它的真实程序中,“h”是一个成员函数,“C”必须是一个嵌套类,这样它就隐含地成为“h”所属类的友元。)

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