Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL Vector comparison issue

Tags:

c++

stl

With a vector defined as std::vector<std::string>, Wondering why the following is valid:

if ( vecMetaData[0] != "Some string" ) 
{
    ...

But not this:

switch ( vecMetaData[1] )
{
    ...

Visual studio complains :

error C2450: switch expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>,
1>            _Ax=std::allocator<char>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
like image 586
Konrad Avatar asked Nov 28 '22 21:11

Konrad


1 Answers

switch() needs an integral type (like int, char, ...)

string is not an integral type, neither does string have an implicit conversion to an integral type, so it can't be used in a switch statement

like image 195
Pieter Avatar answered Dec 05 '22 18:12

Pieter