CLI::CheckedTransformer, throws error on parsing enum with stream operator<< overload and default value defined
When using CLI::CheckedTransformer and parsing an enum that has an operator<< overload for ostream, CLI11 is throwing a CLI::ConversionError when a .default_val is defined. I am using release v2.6.2.
The full code to trigger this is below, but here is an explanation:
app.add_option("--color", color)
->default_val(Color::Red)
->transform(CLI::CheckedTransformer(STR_TO_COLOR_MAP));
The STR_TO_COLOR_MAP maps valid input strings to the enum. When no std::ostream& operator<<(std::ostream& os, Color c) is defined, this works correctly. When defining the overload (which converts the enum to string representing it), however, a ConversionError is thrown when attempting to parse the default_val.
#include "CLI11.hpp"
#include <unordered_map>
#define COLOR_LIST \
X(Red) \
X(Blue) \
X(Green) \
enum class Color {
#define X(name) name,
COLOR_LIST
#undef X
};
const std::unordered_map<std::string, Color> STR_TO_COLOR_MAP {
#define X(name) {#name, Color::name},
COLOR_LIST
#undef X
};
const std::unordered_map<Color, std::string> COLOR_TO_STR_MAP {
#define X(name) {Color::name, #name},
COLOR_LIST
#undef X
};
int main(int argc, char** argv)
{
CLI::App app{"Example"};
Color color;
app.add_option("--color", color)
->default_val(Color::Red)
->transform(CLI::CheckedTransformer(STR_TO_COLOR_MAP));
CLI11_PARSE(app, argc, argv);
std::cout << static_cast<int>(color) << std::endl;
}
// Comment this to stop CLI::ConversionError from being thrown
std::ostream& operator<<(std::ostream& os, Color c) {
auto it = COLOR_TO_STR_MAP.find(c);
if (it != COLOR_TO_STR_MAP.end())
return os << it->second;
return os << std::string("Unknown");
}
g++ -std=c++17 main.cpp -o main
Note that I also tried using ->default_str("Green"), (instead of default_val), and although there is no error thrown, the value written to the variable appears to be random (not the expected underlying integer value of 0,1, or 2)
I was able to avoid this error if I cast the enum to an int before passing it to .default_val:
->default_val(static_cast<int>(Color::Green))
This looks similar to #1258
The tests are failing because the color and other_colour variables are unanitialised. Below is the error trace:
/__w/CLI11/CLI11/tests/NewParseTest.cpp:687:11: error: variable 'color' is not initialized [cppcoreguidelines-init-variables,-warnings-as-errors] 687 | Color color; | ^ /__w/CLI11/CLI11/tests/NewParseTest.cpp:688:11: error: variable 'other_color' is not initialized [cppcoreguidelines-init-variables,-warnings-as-errors] 688 | Color other_color; | ^ 39816 warnings generated. Suppressed 39827 warnings (39814 in non-user code, 13 NOLINT). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
:robot: AI text below :robot:
#1330 does not reproduce on v2.6.2 or current main — it reproduces exactly on v2.6.1. The reporter was almost certainly compiling against an older header than they thought.
What I did: extracted the exact code from the issue (including the operator<< defined after main), built it with both GCC 16 and Apple clang at C++17 against three header sets — current main, the v2.6.2 release single-header, and the v2.6.1 headers.
- main and v2.6.2: clean in every variant — no args (prints 0),
--color Blue(prints 1), and also with thetransform()/default_val()chain order swapped. - v2.6.1: throws exactly the reported error, and it aborts with
terminatebecausedefault_val()throws during setup, beforeCLI11_PARSE's catch:CLI::ConversionError: The value --color is not an allowed value for given default value("Red") produces an error : Could not convert: --color = Red
Mechanism: pre-2.6.2, default_val(Color::Red) stringified the enum through the user's operator<< (giving "Red"), then ran the option callback, which lexical_casts that string back to the enum — and enums only convert from integers, so it fails. This was reported as #1258 (which phlptp already linked as similar; it's really a duplicate) and fixed by PR #1261, released in v2.6.2, by adding a value_string overload (include/CLI/TypeTools.hpp:474 at the v2.6.2 tag) that stringifies enums as their underlying integer, so the default round-trips as "0".
Two side observations:
- The reporter's secondary complaint (
default_str("Green")gives a "random" value) is just the uninitializedColor color;—default_stronly sets the display string and never runs the callback, so nothing is ever written to the variable. That behaves the same on main and is expected (they'd wantforce_callback()... though"Green"still wouldn't convert without the transformer applying to defaults). - Small cosmetic residue of the fix: with the
operator<<defined,--helpshows the default as[0]rather than[Red], sincevalue_stringnow bypasses the stream operator.
So the issue can likely be closed as fixed in v2.6.2 (duplicate of #1258), asking the reporter to verify which header their build actually picked up. Want me to draft a comment for the issue?