使用“typedef”或“使用”定义一个结构 - 哪个最好?

发布于 2025-01-16 02:55:18 字数 505 浏览 5 评论 0 原文

示例结构:

typedef struct tagExportSettings
{
    COLORREF    crHeading{};
    COLORREF    crEvenBack{};
    COLORREF    crOddBack{};
    COLORREF    crHighlight{};
    COLORREF    crDate{};
    COLORREF    crEvent{};
    COLORREF    crEmpty{};
    COLORREF    crNotes{};

} EXPORT_SETTINGS_S;

视觉辅助说:

typedef 可以转换为 using 声明。

更改此代码有什么真正的好处吗?

我在软件中的所有代码都使用 EXPORT_SETTINGS_S 所以我不想破坏该语法。

Sample structure:

typedef struct tagExportSettings
{
    COLORREF    crHeading{};
    COLORREF    crEvenBack{};
    COLORREF    crOddBack{};
    COLORREF    crHighlight{};
    COLORREF    crDate{};
    COLORREF    crEvent{};
    COLORREF    crEmpty{};
    COLORREF    crNotes{};

} EXPORT_SETTINGS_S;

Visual Assist says:

typedef can be converted to using declaration.

Is there any real benefit of making this code change?

All my code in the software uses EXPORT_SETTINGS_S so I don't want to break that syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最佳男配角 2025-01-23 02:55:18

更好的是两者都不使用。一种类型名称就足够了。选择 tagExportSettingsEXPORT_SETTINGS_S 并坚持下去。例子:

struct tagExportSettings
{
    // ...
};

但是,1.我在软件中的所有代码都使用EXPORT_SETTINGS_S

正如我所说,选择任一名称。如果您使用 EXPORT_SETTINGS_S,则将该类命名为 EXPORT_SETTINGS_S

struct EXPORT_SETTINGS_S
{
    // ...
};

如果某些内容仍然引用 tagExportSettings,则重构代码以使用规范名称。


但更一般而言,using 优于 typedef,因为它更具可读性。至少有两个原因:

  1. 使用 typedef 语法,哪个是旧名称、哪个是新别名并不直观:

    typedef new_or_old old_or_new; // old_or_new 是新别名
    

    使用通过熟悉的初始化模式变得直观:

    using直观_别名=直观_旧_名称;
    
  2. typedef 语法对于程序员来说很难解析复合名称,因为别名是“交错的”:

    // void() 的别名
    typedef 无效函数();
    使用函数= void();
    

Even better is to use neither. One type name should be enough. Pick either tagExportSettings or EXPORT_SETTINGS_S and stick with it. Example:

struct tagExportSettings
{
    // ...
};

But, 1. All my code in the software uses EXPORT_SETTINGS_S

As I said, pick either name. If you use EXPORT_SETTINGS_S, then name the class as EXPORT_SETTINGS_S:

struct EXPORT_SETTINGS_S
{
    // ...
};

If something still refers to tagExportSettings, then refactor the code to use the canonical name.


But more generally, using is preferred to typedef because it's more readable. There are at least two reasons for it:

  1. With typedef syntax it isn't intuitive which is the old name and which is the new alias:

    typedef new_or_old old_or_new; // old_or_new is the new alias
    

    using is intuitive through familiar pattern of initialisation:

    using intuitively_alias = intuitively_old_name;
    
  2. typedef syntax is difficult for a programmer to parse in case of compound names because the alias is "interleaved":

    // alias for void()
    typedef void function();
    using function = void();
    
笨笨の傻瓜 2025-01-23 02:55:18

如果您使用 C++ 编写,using 会更好。例如,像这样的模板结构:

template <typename Tp>
struct WrapData
{
    const bool has_value = !std::is_void<Tp>::value;
    typename std::conditional<std::is_void<Tp>::value, uint8_t, Tp>::type value;
    // more properties
};

您可以像这样使用 typedefusing

using void_t = WrapData<void>;
using int_t = WrapData<int>;

// same as using
typedef WrapData<int> tp_int_t;

但如果使用模板,则只有 using 可以工作:


// // compile error
// template<typename Tp>
// typedef WrapData<Tp> TpWrapData;

template<typename Tp>
using UsWrapData = WrapData<Tp>;

If you are writing in C++, using is much better. For example, a template struct like this:

template <typename Tp>
struct WrapData
{
    const bool has_value = !std::is_void<Tp>::value;
    typename std::conditional<std::is_void<Tp>::value, uint8_t, Tp>::type value;
    // more properties
};

You can use typedef or using like this:

using void_t = WrapData<void>;
using int_t = WrapData<int>;

// same as using
typedef WrapData<int> tp_int_t;

But only using can work if use template:


// // compile error
// template<typename Tp>
// typedef WrapData<Tp> TpWrapData;

template<typename Tp>
using UsWrapData = WrapData<Tp>;
影子是时光的心 2025-01-23 02:55:18

假设是纯 C++,这取决于您是否使用原始指针。如果不这样做,则只需使用正确的名称定义结构,而不使用 typedefusing

struct EXPORT_SETTINGS_S
    {...

如果您在 Windows 中经常使用原始指针,那么习惯上不仅定义别名,还定义指针类型:

typedef struct tagExportSettings
{
    ...
} EXPORT_SETTINGS_S, *LPEXPORT_SETTINGS_S;

只有当您习惯 LPXXX“类型”。如果你是一个*人,那么就没有必要。

这些都不是使用的明确案例。

Assuming pure C++, it depends if you're using raw pointers or not. If you don't, then just define the struct under proper name without either typedef or using.

struct EXPORT_SETTINGS_S
    {...

If you're using raw pointers a lot and in Windows, it's customary to define not just alias but a pointer type as well:

typedef struct tagExportSettings
{
    ...
} EXPORT_SETTINGS_S, *LPEXPORT_SETTINGS_S;

That certainly matters only if you are accustomed to LPXXX "types". If you're a * person, then there's no need.

Neither of those are a clear-cut case for using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文