boostorg / boostorg/hana

string length = sizeof(S::get()) / sizeof(S::get()[0]) - 1

Open
#436 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.9k
Forks
225
PR merge metrics
No merged PRs in 30d

Description

https://github.com/boostorg/hana/blob/1aa52a97269c1f57ca779e9deed03d8d49a8796d/include/boost/hana/string.hpp#L98

//////////////////////////////////////////////////////////////////////////
// BOOST_HANA_STRING
//////////////////////////////////////////////////////////////////////////
namespace string_detail {
template
constexpr string
prepare_impl(S, std::index_sequence)
{ return {}; }

template
constexpr decltype(auto) prepare(S s) {
return prepare_impl(s,
std::make_index_sequence{});
}
}

#define BOOST_HANA_STRING(s) \
(::boost::hana::string_detail::prepare([]{ \
struct tmp { \
static constexpr decltype(auto) get() { return s; } \
}; \
return tmp{}; \
}())) \
/**/

Is't that a risky way to calculate length like that `sizeof(S::get()) - 1`? What about `BOOST_HANA_STRING(L"...")`, is that ok or may be potential a bug in the length? I know the string mostly the `char` oriented, but that macro can be used potentially with any string.

Contributor guide

Open the contributing guide

Research direction

Start in include/boost/hana/string.hpp at the string_detail::prepare function and the BOOST_HANA_STRING macro. Check how sizeof(S::get()) - 1 behaves for ordinary and wide string literals, then exercise the macro with both forms. Done means confirming whether the calculation is valid and documenting or regression-testing any issue found.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.