boostorg / system

Boost.org system module
http://boost.org/libs/system
36 stars 85 forks source link

Constexpr default ctor #89

Closed vinniefalco closed 2 years ago

vinniefalco commented 2 years ago

This makes error_code default constructor constexpr in C++11 instead of just C++14

vinniefalco commented 2 years ago

I pushed a fix for defective gcc 4.7.x

pdimov commented 2 years ago
  1. This PR contains unrelated changes and hence can't be merged as-is.
  2. What is the code that fails without this PR and passes with it?
vinniefalco commented 2 years ago
  1. fixed
  2. I have error_code as a member of a struct, contained in objects which are constexpr variables with static storage duration:

https://github.com/vinniefalco/http_proto/blob/cbdbb6e959258eebb8970a9b4f1950709d1998b8/include/boost/http_proto/metadata.hpp#L179

(I switched it to the enum as a temporary workaround)

The three "default" values (what you get with a default-constructed object) are constexpr here (header contains a metadata data member) https://github.com/vinniefalco/http_proto/blob/cbdbb6e959258eebb8970a9b4f1950709d1998b8/include/boost/http_proto/detail/impl/header.ipp#L73

pdimov commented 2 years ago

OK, but what is the simplest self-contained code example that both demonstrates the failure and is representative of your actual code?

vinniefalco commented 2 years ago

OK, but what is the simplest self-contained code example that both demonstrates the failure and is representative of your actual code?

Given this code:

#include <boost/system/error_code.hpp>

struct M
{
    constexpr M() = default;

    boost::system::error_code ec;
};

struct H
{
    constexpr H() = default;

    M m;
};

H const& f()
{
    static constexpr H h;
    return h;
}

The current tip of develop fails to compile it at all under C++11.

Changing error_code::error_code() in System from BOOST_SYSTEM_CONSTEXPR to BOOST_CONSTEXPR will compile under C++11 except for GCC 4.7.x (compiler bug I suppose, I struggled to find a workaround but failed)

Applying this pull request will it compile under C++11, except that it will not be constexpr for GCC 4.7.x. My library does not support anything below 4.8 anyway, so this should be no worse than before my change.