有没有typedef绝对必要的情况? [英] Are there cases where a typedef is absolutely necessary?

查看:171
本文介绍了有没有typedef绝对必要的情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅安全bool成语的以下摘录:

typedef void (Testable::*bool_type)() const;
operator bool_type() const;

是否可以在没有typedef的情况下声明转换函数?以下不编译:

Is it possible to declare the conversion function without the typedef? The following does not compile:

operator (void (Testable::*)() const)() const;


推荐答案

啊,我只记得 identity 元功能。可以写

Ah, I just remembered the identity meta-function. It is possible to write

operator typename identity<void (Testable::*)() const>::type() const;



with the following definition of identity:

template <typename T>
struct identity
{
    typedef T type;
};

您可以认为 identity typedef ,但这个解决方案对我来说是好。

You could argue that identity still uses a typedef, but this solution is "good" enough for me.

这篇关于有没有typedef绝对必要的情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆