Practice mode C++

Which of these types CANNOT be used directly as the condition of a C++ switch statement?

Correct answer A. std::string

Explanation

A switch condition must be of integral or enumeration type, or a class type contextually convertible to one, and every case label must be an integral constant expression. char, long and enums all qualify. std::string is a class with no such conversion, so switch(str) does not compile; string comparisons need an if/else chain or a lookup table such as std::map.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.