-
Notifications
You must be signed in to change notification settings - Fork 883
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
compile-time check if adaptor for type exists #1051
Comments
Here is a small example to check adaptor support. #include <msgpack.hpp>
#include <iostream>
#include <experimental/type_traits>
struct foo {};
struct bar {
int i;
MSGPACK_DEFINE(i);
};
template <typename T, typename Stream>
using msgpack_pack_func = decltype(std::declval<T>().msgpack_pack(std::declval<msgpack::packer<Stream>&>()));
template <typename T, typename Stream>
using has_msgpack_pack = std::experimental::is_detected<msgpack_pack_func, T, Stream>;
// check only intrusive pack adaptor
// maybe there is some way to check non-intrusive adaptor
template <typename T, typename Stream>
void my_pack(Stream& buf, T t) {
if constexpr(
has_msgpack_pack<T, Stream>::value // || non-intrusive checker
) {
msgpack::pack(buf, t);
std::cout << "match" << std::endl;
}
else {
std::cout << "not match" << std::endl;
}
}
int main() {
msgpack::sbuffer buf;
my_pack(buf, foo{});
my_pack(buf, bar{});
my_pack(buf, 1); // not match because only intrusive adaptor check
} |
yes, the non-intrusive check is the challenging part. though i wonder if you have any thoughts about the approach that i've outlined above: my approach is intrusive into the |
Unfortunately, I don't have good idea. The current msgpack-c SFINAE is already been complecated due to support multiple versions of API (v1...v3). I don't want to modify the current SFINAE mechanism. |
unfortunately i don't see any way to achieve this as there is no way for the compiler do distinguish between " |
Ok, I unsderstand you already been tried it and #1051 (comment) describes it. +template < typename T >
+using msgpack_unpack_t
+ = decltype( std::declval< T >().msgpack_unpack( std::declval< msgpack::object::implicit_type >() ) ); msgpack-c need to support C++03. So C++11 code in the library need to be added very carefully. |
let me try to flesh this out in code and we can discuss in a PR. out of curiosity: i wonder what's the reason of using c++03 as minimum requirement in 2023? (e.g. c++11 is around for 10+ years already) |
When I inherit msgpack-c project, C++03 was widely used. Now, I personally don't think so. I personally want to migrate at least C++17. But removing support is very difficult. In my experience at msgpack-c project, I did
Even if they are not removing functionality, basically. I guess removing C++03 support is harder. |
i guess it would be entirely appropriate to kindly ask people who cannot adopt c++11 in 2023 to use an older released version of the library. that said, in 2015 it was still controversial to require c++11 as some organisations use toolchains that are a few years old (just like using c++20 in 2023 is still a bit controversial). |
more a question than a bug, but something that i've been wondering without having found a good answer, yet:
i'd like to detect at compile time via a constexpr function if an adaptor for a specific type exists:
currently we have:
operator()
is either implemented by a specialization or it is using the default implementation at:so afaict we may need two answer two question:
enable_if
to check if theoperator()
can be implemented for a specific type?operator()
exists in order to implementmsgpack_can_pack
?thoughts?
i've done a few experiments:
this would allow me to write something like:
any thoughts on such functionality in the library?
The text was updated successfully, but these errors were encountered: