在现代 C++ 中,是否可以使用与此不同的语法编写成员函数别名:
class C {
int f(int a) { return ++a; }
inline int g(int a) { return f(a); }
};
我尝试了几种方法,但都不起作用:
class C {
int f(int a) { return ++a; }
const auto g1 = f; // error: 'auto' not allowed in non-static class member
auto& g2 = f; // error: 'auto' not allowed in non-static class member
using g3 = f; // error: unknown type name 'f'
};
请不要建议使用预处理器...
我为什么需要这个?因为:
- 我有使用旧成员函数的现有代码,我无法修改它,而新代码必须使用新的成员函数。
- 我想避免额外的调用(即使它可以被优化掉)。
PS:clang 20.1.0 产生的错误