gpt4 book ai didi

C++ 可以将本地类引用传递给函数吗?

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

我想知道是否允许以下​​内容:

template < class C >
void function(C&);

void function() {
class {} local;
function(local);
}

谢谢

最佳答案

目前不允许。但它在 C++0x 中受支持。当前标准在 14.3.1/2

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

也就是说,如果函数也是本地的,那没问题

void f() {
class L {} local;
struct C {
static void function(L &l) {
// ...
}
};
C::function(local);
}

关于C++ 可以将本地类引用传递给函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2662843/

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