Skip to content

Type with explicit operator std::string() cannot be used in add_option #420

@steffenb7333

Description

@steffenb7333

This fails to compile:

#include <iostream>
#include <string_view>
#include <CLI/CLI.hpp>
namespace N
{
	struct Type
	{
		explicit Type(const std::string_view& a_value) : m_value{a_value}
		{}

		explicit operator std::string() const
		{
			return std::string{"op str"};
		}

		std::string m_value;
	};
}

N::Type m_type{"abc"};

int main(int argc, char** argv)
{
	CLI::App app;
	app.add_option("--type", m_type, "type")->capture_default_str();
	CLI11_PARSE(app, argc, argv);

	return EXIT_SUCCESS;
}

The cause is the explicit conversion operator to std::string. Compiles again, if the "explicit" is removed.
May be related to #410.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions