我正在编写一些函数的返回类型相当复杂的代码。我想使用auto
for 从返回类型推导,但这在前向声明中显然是不可能的。所以我希望至少只复制 return 语句的内容并执行以下操作,
int q = 5; // veeery complicated type
/* Declaration - the best we can do */
auto f() -> decltype(q);
/* Later, in a different file */
auto f() {
return q;
}
这会在 GCC 7 中产生以下错误,
error: ambiguating new declaration of ‘auto f()’
note: old declaration ‘int f()’
我当然可以重复
auto f() -> decltype(q) {
return q;
}
return
在定义中(有效),但是当返回类型已经由语句唯一给出时,我为什么需要这样做?我的定义中的类型f
最终如何比 更加模棱两可int f()
?