次のような関数があるとします。
template <typename F, typename... A>
inline void execute(F&& functor, A&& ... args) {
std::forward<decltype(functor)>(functor)(std::forward<decltype(args)>(args)...);
}
これは、単純な非テンプレート関数で機能します。ただし、テンプレート化された関数(非常に工夫された関数)を完全に転送しようとしています。
namespace detail {
template <typename CodecImpl>
class codec
{
public:
//
// Encoding
// Convenient version, returns an std::string.
static std::string encode(const uint8_t* binary, size_t binary_size);
static std::string encode(const char* binary, size_t binary_size);
...
};
class base64_rfc4648
{
public:
template <typename Codec> using codec_impl = stream_codec<Codec, base64_rfc4648>;
static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size() {
static_assert(sizeof(base64_rfc4648_alphabet) == 64, "base64 alphabet must have 64 values");
return sizeof(base64_rfc4648_alphabet);
}
static CPPCODEC_ALWAYS_INLINE constexpr char symbol(alphabet_index_t idx)
{
return base64_rfc4648_alphabet[idx];
}
...
};
} // namespace detail
using base64_rfc4648 = detail::codec<detail::base64<detail::base64_rfc4648>>;
上記を転送しようとしています:
std::string buf("hello world");
execute(base64_rfc4648::encode, buf.c_str(), buf.size());
動作しません。テンプレートの推定が失敗する:
注:テンプレートパラメータ「F」を推測できませんでした
そしてそれはまた注意します:
を呼び出すための一致する関数がありません
'execute(<unresolved overloaded function type>, const char*, std::__cxx11::basic_string<char>::size_type)'
どうすればこれを修正できますか?
注:読みやすくするために上記の情報は短くしましたが、さらに情報が必要な場合は追加できます。
を使用boost::hof
しbase64_rfc4648::encode
て、関数オブジェクトをラップすることができます。execute(BOOST_HOF_LIFT(base64_rfc4648::encode), buf.c_str(), buf.size());
これがBOOST_HOF_LIFTのドキュメントです
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加