Skip to content

Header cleanup with C++11 as a baseline #1364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 18, 2025
Merged
8 changes: 3 additions & 5 deletions .github/workflows/macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
strategy:
matrix:
include:
- {os: macOS-latest}
- {os: macos-latest}
- {os: macos-13}
#- {os: ubuntu-latest}

runs-on: ${{ matrix.os }}
Expand All @@ -24,10 +25,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup
uses: eddelbuettel/github-actions/r-ci-setup@master

- name: Bootstrap
run: ./run.sh bootstrap
uses: eddelbuettel/github-actions/r-ci@master

- name: Dependencies
run: ./run.sh install_deps
Expand Down
16 changes: 16 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
2025-03-15 Dirk Eddelbuettel <[email protected]>

* DESCRIPTION (Version, Date): Roll micro version and date
* inst/include/Rcpp/config.h: Idem

* inst/include/Rcpp/platform/compiler.h: Simplified and shortened
establishing C++11 as baseline; further PRs to complete this
* inst/include/Rcpp/String.h: Unconditionally use std::hash with C++11
* inst/include/Rcpp/wrap.h: Unconditionally use static_assert
* inst/include/Rcpp/sugar/functions/sapply.h: Use std::result_of
(with C++11 or C++14) or std::invoke_result (C++17 or later)
* inst/include/Rcpp/sugar/functions/table.h: Use std::map
* inst/include/Rcpp/sugar/sets.h: Use std::unordered_{set,map}
* man/evalCpp.Rd: Update example
* src/api.cpp: Simplify rcpp_capabilities assignment

2025-03-13 Dirk Eddelbuettel <[email protected]>

* DESCRIPTION (Version, Date): Roll micro version and date
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: Rcpp
Title: Seamless R and C++ Integration
Version: 1.0.14.6
Date: 2025-03-13
Version: 1.0.14.7
Date: 2025-03-15
Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "[email protected]",
comment = c(ORCID = "0000-0001-6419-907X")),
person("Romain", "Francois", role = "aut",
Expand Down
16 changes: 3 additions & 13 deletions inst/include/Rcpp/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,24 +734,14 @@ namespace Rcpp {

} // Rcpp

/** hash can be in std or std::tr1 */
#if defined(RCPP_USING_CXX11) || defined(HAS_TR1)
namespace std
{
#ifndef RCPP_USING_CXX11
namespace tr1 {
#endif
/** hash via std */
namespace std {
template <>
struct hash<Rcpp::String>
{
struct hash<Rcpp::String> {
size_t operator()(const Rcpp::String & s) const{
return hash<string>()(s.get_cstring());
}
};
#ifndef RCPP_USING_CXX11
}
#endif
}
#endif

#endif
4 changes: 2 additions & 2 deletions inst/include/Rcpp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define RCPP_VERSION_STRING "1.0.14"

// the current source snapshot (using four components, if a fifth is used in DESCRIPTION we ignore it)
#define RCPP_DEV_VERSION RcppDevVersion(1,0,14,6)
#define RCPP_DEV_VERSION_STRING "1.0.14.6"
#define RCPP_DEV_VERSION RcppDevVersion(1,0,14,7)
#define RCPP_DEV_VERSION_STRING "1.0.14.7"

#endif
20 changes: 7 additions & 13 deletions inst/include/Rcpp/internal/wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,12 @@ namespace Rcpp {
* quite a cryptic message
*/
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable<%s>(., false )", DEMANGLE(T))
// here we know that T is not convertible to SEXP
#ifdef HAS_STATIC_ASSERT
static_assert(!sizeof(T), "cannot convert type to SEXP");
#else
// leave the cryptic message
SEXP x = object;
return x;
#endif
return R_NilValue; // -Wall
}
inline SEXP wrap_dispatch_unknown_iterable(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable<%s>(., false )", DEMANGLE(T))
// here we know that T is not convertible to SEXP
static_assert(!sizeof(T), "cannot convert type to SEXP");
return R_NilValue; // -Wall
}

template <typename T>
inline SEXP wrap_dispatch_unknown_iterable__logical(const T& object, ::Rcpp::traits::true_type) {
Expand Down Expand Up @@ -936,7 +930,7 @@ namespace Rcpp {
if (v != NULL)
return Rf_mkString(v);
else
return R_NilValue; // #nocov
return R_NilValue; // #nocov
}

/**
Expand Down
181 changes: 18 additions & 163 deletions inst/include/Rcpp/platform/compiler.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// compiler.h: Rcpp R/C++ interface class library -- check compiler
//
// Copyright (C) 2012 - 2013 Dirk Eddelbuettel, Romain Francois, and Kevin Ushey
// Copyright (C) 2012 - 2025 Dirk Eddelbuettel, Romain Francois, and Kevin Ushey
//
// This file is part of Rcpp.
//
Expand All @@ -24,173 +22,30 @@

// NB: A vast list valid identifiers is at these wiki pages:
// http://sourceforge.net/p/predef/wiki/Home/

#undef GOOD_COMPILER_FOR_RCPP
#ifdef __GNUC__
#define GOOD_COMPILER_FOR_RCPP
#endif
#ifdef __SUNPRO_CC
#define GOOD_COMPILER_FOR_RCPP
#endif
#ifdef __clang__
#define GOOD_COMPILER_FOR_RCPP
#endif
#ifdef __INTEL_COMPILER
#define GOOD_COMPILER_FOR_RCPP
#endif

#ifndef GOOD_COMPILER_FOR_RCPP
# error "This compiler is not supported"
#endif

#ifdef __GNUC__
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
// g++ 4.5 does not seem to like some of the fast indexing
#if GCC_VERSION >= 40500
#define IS_GCC_450_OR_LATER
#endif
// g++ 4.6 switches from exception_defines.h to bits/exception_defines.h
#if GCC_VERSION < 40600
#define IS_EARLIER_THAN_GCC_460
#endif
#if GCC_VERSION >= 40600
#define IS_GCC_460_OR_LATER
#endif
#endif

// Check for the presence of C++0x (or later) support
#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
#define RCPP_USING_CXX0X_OR_LATER
#if !defined(__GNUC__) && !defined(__SUNPRO_CC) && !defined(__clang__) && !defined(__INTEL_COMPILER)
#error "This compiler is not supported"
#endif

// Check C++0x/11 features
#if defined(__INTEL_COMPILER)
#if __cplusplus >= 201103L
#define RCPP_USING_CXX11
#if __INTEL_COMPILER >= 1210
#define HAS_VARIADIC_TEMPLATES
#endif
#if __INTEL_COMPILER >= 1100
#define HAS_STATIC_ASSERT
#endif
#endif
#elif defined(__clang__)
#if __cplusplus >= 201103L
#define RCPP_USING_CXX11
#if __has_feature(cxx_variadic_templates)
#define HAS_VARIADIC_TEMPLATES
#endif
#if __has_feature(cxx_static_assert)
#define HAS_STATIC_ASSERT
#endif
#endif
#elif defined(__GNUC__)
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#if GCC_VERSION >= 40300
#define HAS_VARIADIC_TEMPLATES
#define HAS_STATIC_ASSERT
#endif
#endif
#if GCC_VERSION >= 40800 && __cplusplus >= 201103L
#define RCPP_USING_CXX11
#endif
// Simpler test and minimal standard: C++11 or else we die
#if __cplusplus < 201103L
#error "The C++ compilation standard is too old: use C++11 or newer."
#endif

// Check C++0x headers
// C++11 features -- that used to be carefully tested for or worked around via CXX0X / TR1
// These defines are all planned to get removed just how a number have already been removed. One at a time...
#define RCPP_USING_CXX11
#define HAS_VARIADIC_TEMPLATES
#include <cmath>
#if defined(__INTEL_COMPILER) || (defined(__GNUC__) && !defined(__clang__))
#if defined(__GLIBCXX__) && defined(__GXX_EXPERIMENTAL_CXX0X__)
#if GCC_VERSION >= 40400
#define HAS_CXX0X_UNORDERED_MAP
#define HAS_CXX0X_UNORDERED_SET
#define HAS_CXX0X_INITIALIZER_LIST
#endif
#endif
#elif defined(__clang__)
#if __cplusplus >= 201103L
#if __has_include(<unordered_map>)
#define HAS_CXX0X_UNORDERED_MAP
#endif
#if __has_include(<unordered_set>)
#define HAS_CXX0X_UNORDERED_SET
#endif
#if __has_include(<initializer_list>)
#define HAS_CXX0X_INITIALIZER_LIST
#endif
#endif
#endif

// Check TR1 Headers
#if defined(__INTEL_COMPILER) || (defined(__GNUC__) && !defined(__clang__))
#if defined(__GLIBCXX__)
#if GCC_VERSION >= 40400 || ( GCC_VERSION >= 40201 && defined(__APPLE__) )
#define HAS_TR1_UNORDERED_MAP
#define HAS_TR1_UNORDERED_SET
#endif
#endif
#elif defined(__clang__)
#if __cplusplus >= 201103L
#if __has_include(<tr1/unordered_map>)
#define HAS_TR1_UNORDERED_MAP
#endif
#if __has_include(<tr1/unordered_set>)
#define HAS_TR1_UNORDERED_SET
#endif
#endif
#endif

#if defined(HAS_TR1_UNORDERED_MAP) && defined(HAS_TR1_UNORDERED_SET)
#define HAS_TR1
#endif

// Conditionally include headers
#ifdef HAS_CXX0X_INITIALIZER_LIST
#include <initializer_list>
#endif

#ifdef RCPP_USING_CXX11
#if defined(HAS_CXX0X_UNORDERED_MAP)
#include <unordered_map>
#define RCPP_USING_UNORDERED_MAP
#define RCPP_UNORDERED_MAP std::unordered_map
#else
#include <map>
#define RCPP_USING_MAP
#define RCPP_UNORDERED_MAP std::map
#endif
#if defined(HAS_CXX0X_UNORDERED_SET)
#include <unordered_set>
#define RCPP_USING_UNORDERED_SET
#define RCPP_UNORDERED_SET std::unordered_set
#else
#include <set>
#define RCPP_USING_SET
#define RCPP_UNORDERED_SET std::set
#endif
#else
#if defined(HAS_TR1_UNORDERED_MAP)
#include <tr1/unordered_map>
#define RCPP_USING_TR1_UNORDERED_MAP
#define RCPP_UNORDERED_MAP std::tr1::unordered_map
#else
#include <map>
#define RCPP_USING_MAP
#define RCPP_UNORDERED_MAP std::map
#endif
#if defined(HAS_TR1_UNORDERED_SET)
#include <tr1/unordered_set>
#define RCPP_USING_TR1_UNORDERED_SET
#define RCPP_UNORDERED_SET std::tr1::unordered_set
#else
#include <set>
#define RCPP_USING_SET
#define RCPP_UNORDERED_SET std::set
#endif
#endif

#ifdef __GNUC__
#include <unordered_map>
#define RCPP_USING_UNORDERED_MAP
#define RCPP_UNORDERED_MAP std::unordered_map
#include <unordered_set>
#define RCPP_USING_UNORDERED_SET
#define RCPP_UNORDERED_SET std::unordered_set

#if defined(__GNUC__)
#define RCPP_HAS_DEMANGLING
#endif


#endif
10 changes: 5 additions & 5 deletions inst/include/Rcpp/sugar/functions/sapply.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
#ifndef Rcpp__sugar__sapply_h
#define Rcpp__sugar__sapply_h

#if defined(RCPP_USING_CXX0X_OR_LATER)
#include <type_traits> // ::std::result_of
#endif
// This used to be conditional on a define and test in compiler.h
#include <type_traits> // ::std::result_of

namespace Rcpp{
namespace sugar{

template <typename Function, typename SugarExpression>
struct sapply_application_result_of
{
#if defined(RCPP_USING_CXX0X_OR_LATER)
#if __cplusplus >= 201103L
#if __cplusplus < 201703L
// deprecated by C++17, removed by C++2020, see https://en.cppreference.com/w/cpp/types/result_of
typedef typename ::std::result_of<Function(typename SugarExpression::stored_type)>::type type;
Expand All @@ -40,9 +39,10 @@ struct sapply_application_result_of
typedef typename ::std::invoke_result<Function, typename SugarExpression::stored_type>::type type;
#endif
#else
// TODO this else branch can likely go
typedef typename ::Rcpp::traits::result_of<Function>::type type;
#endif
} ;
};

// template <typename Function, typename SugarExpression>
// using sapply_application_result_of_t = typename sapply_application_result_of<Function, SugarExpression>::type;
Expand Down
Loading