std::string (std::basic_string) have assignment operator for 'char' type. But, for this reason, std::string may assign any integral types. See little example.
#include <string>
enum MyEnum{ Va = 0, Vb = 2, Vc = 4 };
int main(){
std::string s;
s = 'a'; // (1) OK - logical.
s = Vc; // (2) Ops. Compiled without any warnings.
s = true; // (3) Ops....
s = 23; // (4) Ops...
}
Q: How disable (or add warning ) (2, 3, 4) situations ??
Given the constraints of C++03 and GCC 4.8 as in the tags, I could not get
-Wconversionto do anything useful (and in GCC 7 it doesn't even generate the warnings for me despite telling it that I'm using--std=c++03).As such, there's a good practical solution that requires only minimal change at your calling site:
Proxy the assignment via a class object that wraps your string and that allows assignment from
charbut disallows it fromint:However, if your final goal is to specifically get warnings or errors when using std::string, your best option in C++03 is to patch the
<string>header to add the private int-assignment operator shown in the class above. But that means patching a system header and the procedure and results will be dependant on your compiler version (and will have to be repeated in each installation and compiler version).