Skip to content

Leaf allows C-style string as error objects #84

@ericli-splunk

Description

@ericli-splunk

Version of Boost

1.83.0

Actual and Expected Behavior

When running the following program

#include <string>
#include <iostream>
#include <boost/leaf/result.hpp>
#include <boost/leaf/handle_errors.hpp>

using namespace boost;

int main () {
    leaf::try_handle_all(
        []() -> leaf::result<void> {
            boost::leaf::throw_exception("foo");
        },
        [](std::string err) {
            std::cout << "std::string: " << err << std::endl;
        },
        [](const char* err) {
            std::cout << "const char*: " << (void *)err << std::endl;
        },
        []() {
            std::cout << "general" << std::endl;
        }
    );
    return 0;
}

Actual output:

const char*: 0

Expected Behavior:

  • Compile error on line boost::leaf::throw_exception("foo"); saying something like "Error objects of pointer types are not allowed"

Explanation

Some people may expect the program to output "std::string: foo" or "const char*: 0x12345678". However, this is not the case because Leaf does not support pointer type error objects. Thus, I think my code should not compile.

It appears that the following code is designed to prevent my code from compiling

static_assert(!std::is_pointer<E>::value, "Error objects of pointer types are not allowed");

However, my code compiles fine because e's type is const char (&)[4], not const char *. The latter is a pointer type, but the former is not.

Steps necessary to reproduce the problem

Compile and run using g++ a.cpp -o a -g && ./a

All relevant compiler information

g++ (Debian 14.2.0-19) 14.2.0

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