Home>
Question
scoped enum type variables are recognized as values other than the enumerator, but if you define the following functionfunc
, you will get the following warning:
Visual Studio:Warning: C4715: Some control paths do not return a value.
GCC:warning: control reaches end of non-void function [-Wreturn-type]
Is this a case where in some case there is a case where no if statement is caught and not returned?
enum class Type
{
A,
B,
C,
};
int func (Type type)
{
if (type == Type :: A)
return 1;
else if (type == Type :: B)
return 2;
else if (type == Type :: C)
return 3;
}
int main ()
{
func (Type :: A);
}
Environment
- C ++ 11
-
Answer # 1
-
Answer # 2
Even though it is a
enum class
, the contents are integers, so it was possible to usestatic_cast
to inject values that are not related to brute force. I have n’t checked.)# include
using namespace std; enum class Type { A, B, C, }; int main (void) { // out of range because there are no 7 Type t = static_cast (7); // Ichio "7" returns as it is cout< (t)< Example of operation in paiza.io
>Is there such a pattern?
enum class Type { A = 2, B = 7, C = 9, }; // Non-local and not explicitly writing initial value Type t;// The value is not initialized to 0 int main () { std :: out<
(t)<
Related articles
- c ++ - is it possible to implement random forest on a microcomputer?
- when calling a dll created with the c ++ cli with c ++, is it possible to do an implicit link?
- [powershell] when using the import-csv cmdlet to store values in variables line by line, object properties are also stored as
- java - is it possible to include variables in kotolin variable names?
- c ++: about accessing instance variables from instance methods
- by doing cin, variables that should be displayed first are overwritten c ++
- c ++ - what are the names of functions and variables that are frequently used in game development?
- nuxtjs - how to put vuex values into variables when page reloads
- unity - is it possible to add animation values
- c ++ - how to share class variables
- How Python assigns function values to variables
- [javascript] is it possible to use variables for function names?
- is it possible to put parameter pack in front in c ++ 17?
- is it possible to hide the note included in the error message when compiling c ++ with clang?
- c ++ - definition position of variables
- c # - is it possible to set multiple values for one variable?
- c ++ - is it possible to use a function within a function?
- ruby - how to sort the values of rails variables in ascending order
- html - i want to pass variables or call values from the database in the same controller
- c ++ - is it possible to format simple lines for () and if () at the same time with vscode clang-format?
Trends
Hello.
I think the compiler hasn't just checked that it can't take values other than the enumerator.
In switch-case, I think that there were some compilers that checked that much.
Checking with an if statement is quite difficult, but it is easy with switch-case.