boostorg / static_string

A fixed capacity dynamically sized string
http://boost.org/libs/static_string
Boost Software License 1.0
63 stars 29 forks source link

Compilation error under c++20 with constexpr #30

Closed maksymilian-palka closed 2 years ago

maksymilian-palka commented 2 years ago

Hello,

I have compilation errors for the following code:

#include <iostream>
#include <boost/static_string.hpp>

constexpr auto getString()
{
    boost::static_strings::static_string<1024> content;
    content.append("Hello world");
    return content;
}

constexpr auto msg = getString();

int main()
{
    std::cout << msg.c_str() << std::endl;
    return 0;
}

The code fails to compile under:

The walk around I've found is to allow an array initialisation of static_string_base class, meaning replacing:

#ifdef BOOST_STATIC_STRING_CPP20
    value_type data_[N + 1];
#else
    value_type data_[N + 1] {};
#endif

with

    value_type data_[N + 1] {};
beached commented 2 years ago

It looks like the issue is that boost config is using BOOST_STATIC_STRING_STANDARD_VERSION to determine if BOOST_STATIC_STRING_CPP20 is defined, which is essentially cplusplus with fixes for MSVC. This does not tell us that the compiler supports anything in C++20 other than the -std=c++20 flag has been set. I think for the init stuff in C++20, there is a feature flag set by the compiler `cpp_constexpr_dynamic_alloc >= 201907L`, but not 100% that is the correct flag.

WPMGPRoSToTeMa commented 2 years ago

@beached it should be __cpp_constexpr >= 201907L. https://en.cppreference.com/w/cpp/feature_test

beached commented 2 years ago

Ah ok, I see now. I thought it might have came with the allocator support in More Constexpr Containers, I didn't know about Permitting trivial default initialization in constexpr contexts

alandefreitas commented 2 years ago

This should have been fixed in #32