Skip to content

Commit 2d598be

Browse files
committed
🆕 ⬆️ [NTTP] Support for ""_s using C++20 non-type template parameters
Problem: - ""_s is using a GNU extension to produce an aux::string. Solution: - C++20 `non-type template parameters` instead if available. Note: - That makes SML C++20 standard compliant.
1 parent da8a0cc commit 2d598be

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

include/boost/sml.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,20 @@ const char *get_type_name() {
466466
return detail::get_type_name<T, 63>(__PRETTY_FUNCTION__, make_index_sequence<sizeof(__PRETTY_FUNCTION__) - 63 - 2>{});
467467
#endif
468468
}
469+
#if defined(__cpp_nontype_template_parameter_class)
470+
template <auto N>
471+
struct fixed_string {
472+
static constexpr auto size = N;
473+
char data[N + 1]{};
474+
constexpr fixed_string(char const *str) {
475+
for (auto i = 0; i < N; ++i) {
476+
data[i] = str[i];
477+
}
478+
}
479+
};
480+
template <auto N>
481+
fixed_string(char const (&)[N]) -> fixed_string<N - 1>;
482+
#endif
469483
template <class T, T...>
470484
struct string;
471485
template <char... Chrs>
@@ -2614,7 +2628,18 @@ template <class T>
26142628
typename front::state_sm<T>::type state __BOOST_SML_VT_INIT;
26152629
#endif
26162630
inline namespace literals {
2617-
#if !(defined(_MSC_VER) && !defined(__clang__))
2631+
#if defined(__cpp_nontype_template_parameter_class)
2632+
template <aux::fixed_string Str>
2633+
constexpr auto operator""_s() {
2634+
return []<auto... Ns>(aux::index_sequence<Ns...>) { return front::state<aux::string<char, Str.data[Ns]...>>{}; }
2635+
(aux::make_index_sequence<Str.size>{});
2636+
}
2637+
template <aux::fixed_string Str>
2638+
constexpr auto operator""_e() {
2639+
return []<auto... Ns>(aux::index_sequence<Ns...>) { return event<aux::string<char, Str.data[Ns]...>>; }
2640+
(aux::make_index_sequence<Str.size>{});
2641+
}
2642+
#elif !(defined(_MSC_VER) && !defined(__clang__))
26182643
template <class T, T... Chrs>
26192644
constexpr auto operator""_s() {
26202645
return front::state<aux::string<T, Chrs...>>{};

include/boost/sml/aux_/utility.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,22 @@ const char *get_type_name() {
315315
#endif // __pph__
316316
}
317317

318+
#if defined(__cpp_nontype_template_parameter_class) // __pph__
319+
template <auto N>
320+
struct fixed_string {
321+
static constexpr auto size = N;
322+
char data[N + 1]{};
323+
324+
constexpr fixed_string(char const *str) {
325+
for (auto i = 0; i < N; ++i) {
326+
data[i] = str[i];
327+
}
328+
}
329+
};
330+
template <auto N>
331+
fixed_string(char const (&)[N]) -> fixed_string<N - 1>;
332+
#endif // __pph__
333+
318334
template <class T, T...>
319335
struct string;
320336

include/boost/sml/transition_table.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ typename front::state_sm<T>::type state __BOOST_SML_VT_INIT;
5555
#endif // __pph__
5656

5757
inline namespace literals {
58-
#if !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
58+
#if defined(__cpp_nontype_template_parameter_class) // __pph__
59+
template <aux::fixed_string Str>
60+
constexpr auto operator""_s() {
61+
return []<auto... Ns>(aux::index_sequence<Ns...>) { return front::state<aux::string<char, Str.data[Ns]...>>{}; }
62+
(aux::make_index_sequence<Str.size>{});
63+
}
64+
template <aux::fixed_string Str>
65+
constexpr auto operator""_e() {
66+
return []<auto... Ns>(aux::index_sequence<Ns...>) { return event<aux::string<char, Str.data[Ns]...>>; }
67+
(aux::make_index_sequence<Str.size>{});
68+
}
69+
#elif !(defined(_MSC_VER) && !defined(__clang__)) // __pph__
5970
template <class T, T... Chrs>
6071
constexpr auto operator""_s() {
6172
return front::state<aux::string<T, Chrs...>>{};
@@ -64,8 +75,7 @@ template <class T, T... Chrs>
6475
constexpr auto operator""_e() {
6576
return event<aux::string<T, Chrs...>>;
6677
}
67-
#endif // __pph__
68-
78+
#endif // __pph__
6979
} // literals
7080

7181
__BOOST_SML_UNUSED static front::state<back::terminate_state> X;

0 commit comments

Comments
 (0)