有没有一种方法可以在不输入类名的情况下获取类类型(宏扩展的示例)?
我希望 decltype(*this)
可从类声明中获得,但事实并非如此。
// For example, I want declare a swap from macro expansion without passing class name on macro argument
#define SWAP_METHOD()
void swap( decltype(*this)& a1, decltype(*this)& a2 )
class A
{
SWAP_METHOD();
}
最佳答案
使用模板元编程的解决方案。我认为这是最简单的解决方案。
#include <utility>
#define SWAP_METHOD() \
template <typename T> \
void swap(T& a1, T& a2)
class A {
public:
SWAP_METHOD();
};
template <>
void A::swap(A& a1, A& a2) {
std::swap(a1, a2);
}
int main() {
A a, b, c;
a.swap(b, c);
return 0;
}
关于c++ - 如何在类声明中获取类的 decltype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64708089/