Skip to content

Instantly share code, notes, and snippets.

@oliora
Last active May 17, 2018 16:40
Show Gist options
  • Save oliora/f26694bb61d631687dfe1a1646351898 to your computer and use it in GitHub Desktop.
Save oliora/f26694bb61d631687dfe1a1646351898 to your computer and use it in GitHub Desktop.
Function fallback
std::string wstr_to_utf8(boost::wstring_view);
namespace detail {
template<class T, class = void>
struct narrow_impl
{
template<class U>
static U&& invoke(U&& s) noexcept
{
static_assert(std::is_same<T, U&&>::value, "error");
return std::forward<U>(s);
}
};
template<class T>
struct narrow_impl<T, void_t<decltype(wstr_to_utf8(std::declval<T>()))>>
{
template<class U>
static auto invoke(U&& s) -> decltype(wstr_to_utf8(std::forward<U>(s)))
{
static_assert(std::is_same<T, U&&>::value, "error");
return wstr_to_utf8(std::forward<U>(s));
}
};
}
template<class T>
inline auto narrow(T&& s) -> decltype(detail::narrow_impl<T&&>::invoke(std::forward<T>(s)))
{
return detail::narrow_impl<T&&>::invoke(std::forward<T>(s));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment