Skip to content

Fix C++11/C++14 build with -fno-exceptions#88

Merged
martinmoene merged 1 commit intononstd-lite:masterfrom
viordash:fix-compile-error
Oct 8, 2025
Merged

Fix C++11/C++14 build with -fno-exceptions#88
martinmoene merged 1 commit intononstd-lite:masterfrom
viordash:fix-compile-error

Conversation

@viordash
Copy link
Contributor

@viordash viordash commented Oct 8, 2025

Problem:

  • Compilation fails with C++11/C++14 and -fno-exceptions due to missing include for std::terminate().

STR:

$ g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0

$ g++ -std=c++14 -Wall -Wextra -fno-exceptions -g -O0 -o 01-basic.exe 01-basic.cpp && ./01-basic.exe
In file included from 01-basic.cpp:1:
nonstd/span.hpp: In function ‘void nonstd::span_lite::detail::report_contract_violation(const char*)’:
nonstd/span.hpp:863:10: error: ‘terminate’ is not a member of ‘std’
  863 |     std::terminate();
      |          ^~~~~~~~~

Solution:

  • Add #include when using std::terminate() in contract violation handling.

@martinmoene martinmoene merged commit 954f5db into nonstd-lite:master Oct 8, 2025
8 checks passed
@viordash viordash deleted the fix-compile-error branch October 8, 2025 15:36
@tjenssen
Copy link

tjenssen commented Jan 27, 2026

With exceptions enabled (span_CONFIG_NO_EXCEPTIONS == 0) and default contract behavior
(span_CONFIG_CONTRACT_VIOLATION_THROWS_V == 0), report_contract_violation() calls
std::terminate(), but is not included (only stdexcept).


137:
#ifndef span_CONFIG_NO_EXCEPTIONS
# if defined(_MSC_VER)
#  include <cstddef>    // for _HAS_EXCEPTIONS
# endif
# if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
#  define span_CONFIG_NO_EXCEPTIONS  0
# else
#  define span_CONFIG_NO_EXCEPTIONS  1
#  undef  span_CONFIG_CONTRACT_VIOLATION_THROWS
#  undef  span_CONFIG_CONTRACT_VIOLATION_TERMINATES
#  define span_CONFIG_CONTRACT_VIOLATION_THROWS  0
#  define span_CONFIG_CONTRACT_VIOLATION_TERMINATES  1
# endif
#endif

166:
#if    defined( span_CONFIG_CONTRACT_VIOLATION_THROWS )
# define        span_CONFIG_CONTRACT_VIOLATION_THROWS_V  span_CONFIG_CONTRACT_VIOLATION_THROWS
#else
# define        span_CONFIG_CONTRACT_VIOLATION_THROWS_V  0
#endif


494:
#if ! span_CONFIG( NO_EXCEPTIONS )
# include <stdexcept>
#elif ! span_CONFIG( CONTRACT_VIOLATION_THROWS_V )
# include <exception>
#endif

844:
#if span_CONFIG( CONTRACT_VIOLATION_THROWS_V )

struct contract_violation : std::logic_error
{
    explicit contract_violation( char const * const message )
        : std::logic_error( message )
    {}
};

inline void report_contract_violation( char const * msg )
{
    throw contract_violation( msg );
}

#else // span_CONFIG( CONTRACT_VIOLATION_THROWS_V )

span_noreturn inline void report_contract_violation( char const * /*msg*/ ) span_noexcept
{
    std::terminate();
}

@martinmoene
Copy link
Collaborator

@tjenssen Thanks for the heads-up, I'll have a look at it.

martinmoene added a commit that referenced this pull request Jan 31, 2026
martinmoene added a commit that referenced this pull request Jan 31, 2026
martinmoene added a commit that referenced this pull request Jan 31, 2026
martinmoene added a commit that referenced this pull request Jan 31, 2026
martinmoene added a commit that referenced this pull request Jan 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants