std::string
has an overload for operator= that takes a character. When you pass an argument to a function by value (i.e, an operator), copy initialization occurs. In copy initialization, standard conversions, also known as an "implicit conversion", may be used to the convert the value. In this case, your double is being silently converted to a char so that it may be used in operator=
.
For GCC, -Wall -Wextra -pedantic
will not make a diagnostic appear. You can try -Wfloat-conversion
, which is enabled by -Wconversion
. Example:
main.cpp:11:10: warning: conversion to 'char' alters 'double' constant value
[-Wfloat-conversion]
a = 3.2;
Alternatively, use braces to force a narrowing conversion error.
s = {4.3};
// warning: narrowing conversion of '4.2e+1' from 'double' to 'char' inside { }
// [-Wnarrowing]