C enum应该以分号结束返回函数

时间:2022-09-10 23:31:02

I ask this question because of release of new JetBrains product CLion. I started testing new C/C++ ide and when I typed simple function that return enum, Clion (JetBrains) put automatically the semicolon ";" at the end of closing brackets.

我问这个问题是因为发布了新的JetBrains产品CLion。我开始测试新的C / C ++ ide,当我输入返回枚举的简单函数时,Clion(JetBrains)自动输入分号“;”在右括号末尾。

 enum Error someCode(void)
 {
    //some code
 }; //<- semicolon at end

but after non enum function

但是在非枚举功能之后

void nonEnumFunct(void)
{
   //some code
} //<- without semicolon

I never consider that before (because I have a lot of respect to JetBrains products :) butI think CLion misunderstood that the someCode is function that returns enum but not enum. Because function should not have ";" at the end even if it return enum structure.

我之前从未考虑过(因为我非常尊重JetBrains产品:)但我认为CLion误解了someCode是返回枚举而不是枚举的函数。因为功能不应该有“;”最后,即使它返回枚举结构。

My question is: if it is good practise to declare function as returning enum or shoud I always declare enum as:

我的问题是:如果将函数声明为返回枚举或shoud是好的做法,我总是将枚举声明为:

typedef enum some_enum {} AnEnum; 

and then use it as returning type in function declaration.

然后在函数声明中将其用作返回类型。

3 个解决方案

#1


2  

It's not a question of good or bad practice.

这不是好的或坏的做法的问题。

Semicolons are simply not allowed in C after a function definition.

在函数定义之后,C中根本不允许使用分号。

Regarding the question of whether you should typedef or not enum types, there is no right answer, it's a matter of taste. I personally prefer not to typedef enum types as I consider typedef (except in some cases) as an obfuscation.

关于你是否应该键入def或者不是enum类型的问题,没有正确答案,这是一个品味问题。我个人更喜欢不输入def枚举类型,因为我认为typedef(在某些情况下除外)是混淆。

#2


1  

It's a bug in the IDE (http://youtrack.jetbrains.com/issue/CPP-1027). It should not be there.

这是IDE中的一个错误(http://youtrack.jetbrains.com/issue/CPP-1027)。它不应该存在。

#3


0  

Semicolons are not needed after function definitions (not even after such ones), and they are not encouraged by any style guide I know.

在函数定义之后不需要分号(甚至在这些函数之后也不需要分号),并且我知道任何样式指南都不鼓励它们。

Most probably this as a bug in your IDE. It tries to be smart to predict that your are declaring a variable or a type. But it's wrong.

最有可能这是你的IDE中的一个错误。它试图明智地预测你正在声明变量或类型。但这是错的。

#1


2  

It's not a question of good or bad practice.

这不是好的或坏的做法的问题。

Semicolons are simply not allowed in C after a function definition.

在函数定义之后,C中根本不允许使用分号。

Regarding the question of whether you should typedef or not enum types, there is no right answer, it's a matter of taste. I personally prefer not to typedef enum types as I consider typedef (except in some cases) as an obfuscation.

关于你是否应该键入def或者不是enum类型的问题,没有正确答案,这是一个品味问题。我个人更喜欢不输入def枚举类型,因为我认为typedef(在某些情况下除外)是混淆。

#2


1  

It's a bug in the IDE (http://youtrack.jetbrains.com/issue/CPP-1027). It should not be there.

这是IDE中的一个错误(http://youtrack.jetbrains.com/issue/CPP-1027)。它不应该存在。

#3


0  

Semicolons are not needed after function definitions (not even after such ones), and they are not encouraged by any style guide I know.

在函数定义之后不需要分号(甚至在这些函数之后也不需要分号),并且我知道任何样式指南都不鼓励它们。

Most probably this as a bug in your IDE. It tries to be smart to predict that your are declaring a variable or a type. But it's wrong.

最有可能这是你的IDE中的一个错误。它试图明智地预测你正在声明变量或类型。但这是错的。