考虑以下 safe bool idiom 的摘录:
typedef void (Testable::*bool_type)() const;
operator bool_type() const;
是否可以在没有typedef的情况下声明转换函数?以下不编译:
operator (void (Testable::*)() const)() const;
最佳答案
啊,我只记得 identity
元函数。可以写
operator typename identity<void (Testable::*)() const>::type() const;
identity
的定义如下:
template <typename T>
struct identity
{
typedef T type;
};
您可能会争辩说 identity
仍然使用 typedef
,但这个解决方案对我来说已经足够“好”了。
关于c++ - 是否存在绝对需要 typedef 的情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6998750/