gpt4 book ai didi

c++ - 是否存在绝对需要 typedef 的情况?

转载 作者:IT老高 更新时间:2023-10-28 12:33:48 26 4
gpt4 key购买 nike

考虑以下 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/

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