使用 typedef 作为促进替代类名的一种方式是否被滥用? [英] Is using typedef as a way to facilitate alternate class names a misuse?

查看:38
本文介绍了使用 typedef 作为促进替代类名的一种方式是否被滥用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 C++ 库中,在许多模块中,我像这样使用 typedef:

In my C++ library, throughout many modules, I use typedef like this:

class ClassName {
//...
}

typedef ClassName AlternateClassName;

我这样做只是为了让我自己和可能的其他人允许我的课程以官方名称以外的名称命名——只不过是同义词.这是 typedef 的正确用法吗?

I do this to simply enable myself and potentially others to allow my classes to be called by names other than their official ones--nothing more than a synonym. Is this an OK use of typedef?

推荐答案

typedef 在这里很好,但它的现代替代 using 更好.using 使用更常用的从左到右的语法,也可以模板化

typedef is fine here but its modern replacement using is better. using uses the more usual left-to-right syntax and it can also be templated

template <typename T>
using vec_size_type = typename std::vector<T>::size_type;

vec_size_type<int> sz;

typedef 的替代方法很笨拙:

template <typename T>
struct vec_size_type{
    typedef typename std::vector<T>::size_type type;
};

vec_size_type<int>::type sz;

这篇关于使用 typedef 作为促进替代类名的一种方式是否被滥用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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