-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[struct_pack] better-read and develop,make code clean #309
Comments
template <typename Type>
concept dynamic_span = span<Type> && std::is_same_v<std::integral_constant<std::size_t,Type::extent>,
std::integral_constant<std::size_t,SIZE_MAX>>; After template<typename Type>
concept dynamic_span = requires
{
span<Type>;
std::is_same_v<std::integral_constant<std::size_t, Type::extent>,
std::integral_constant<std::size_t, std::dynamic_extent>>;
}; Comment
|
template<typename Type>
concept continuous_container =
string <Type> || (container <Type> && requires(
Type container
) {
std::span{
container};
}); Because most of the developers comes from China, here is a reasons in Chinese |
struct_pack/reflection : varint concept -------------------------------------------------------------old ------------------------------------ template <typename Type>
constexpr inline bool is_variant_v = false;
template <typename... args>
constexpr inline bool is_variant_v<std::variant<args...>> = true;
template <typename T>
concept variant = is_variant_v<T>; In cpp 17 , you can use variant_size_v to find how many types in varint , so you can know whether the Type T is std::varint template <typename T>
concept variant = std::variant_size_v<T> >= 0; // use variant_size_v check whether the Type T is std::varint it will be easy and clean . ----------------------------------------------------------- new ------------------------------------- #include <iostream>
#include <variant>
struct Person {};
template <typename T>
concept is_variant_variant_size_value = requires {
std::variant_size<T>::value;
};
template <typename T>
concept is_variant_variant_size_v = requires {
std::variant_size_v<T> ;
};
std::variant<int> ans;
int main() {
std::cout << std::boolalpha;
std::cout << is_variant_variant_size_value<decltype(ans)> << std::endl;
std::cout << is_variant_variant_size_value<Person> << std::endl;
std::cout << is_variant_variant_size_value<int> << std::endl;
std::cout << is_variant_variant_size_v<decltype(ans)> << std::endl;
// std::cout << is_variant_variant_size_v<Person> << std::endl; //error
// std::cout << is_variant_variant_size_v<int> << std::endl; //error
return 0;
} after test ,I know std::variant_size::value; is the correct method to do this . |
simplify: template <class T>
concept dynamic_span = span<Type> && T::extent == std::dynamic_extent; |
template Here is the code to compare two integer value. T::extent == std::dynamic_extent; only check the value , but not check type.
|
Put type trait in |
菜英文233 |
See #278 |
#include <iostream>
constexpr std::size_t size_t_v{};//会调用到不同的类型 int32 int64
constexpr float float_v{};
constexpr double double_v{};
class MyPerson
{
public:
MyPerson() = default;
void resize(std::size_t index)
{
}
};
template<typename T>
concept size_t_param =
requires(T string)
{
string.resize(0);
string.resize(size_t_v);
string.resize(float_v);
string.resize(double_v);
};
template<typename T>
requires size_t_param<T>
class Chindren
{
};
int main() {
std::cout << std::boolalpha;
Chindren<MyPerson> Tom ;
return 0;
}
//判断是否有resize接口,可以传入许多不同的参数
//判断有resize接口 且 接口类型为 sizet类型的参数在模板编程中似乎是做不到的 To determine if there is a resize interface , you can use various types of arguments So I think use |
I think your opinion is better. resons :
template <typename Type>
concept span = container < Type> && requires(Type t) {
t = Type{(typename Type::value_type *)nullptr, std::size_t{}};
t.subspan(std::size_t{}, std::size_t{});
};
template <typename Type>
concept dynamic_span = span<Type> && std::is_same_v<std::integral_constant<std::size_t, Type::extent>, std::integral_constant<std::size_t, SIZE_MAX>>;
template <typename Type>
concept static_span = span<Type> && !dynamic_span<Type>;
I think it is a logical bug in yalanting now . Thank you for inspiring me and your comment ! I want to change the code like this , based on the results of our discussion #include "span"
constexpr std::size_t size_t_v{};
template <typename Type>
concept span = container<Type> && requires(Type t) {
// static size_t member : extent
std::integral_constant<std::size_t, Type::extent>{};
t = Type{(typename Type::value_type *)nullptr, size_t_v};
t.subspan(size_t_v, size_t_v);
};
template <typename Type>
concept dynamic_span = span<Type> && Type::extent == std::dynamic_extent;
template <typename Type>
concept static_span = span<Type> && !dynamic_span<Type>; |
You can see in the yalanting website or doc . Currently, yalanting only support std::pair. template<typename Type>
concept pair = std::is_same_v<std::remove_cvref_t<Type>,
std::pair<typename std::remove_cvref_t<Type>::first_type,
typename std::remove_cvref_t<Type>::second_type >>; Here is the code for test #include <iostream>
#include <utility>
template<typename Type>
concept pair = std::is_same_v<std::remove_cvref_t<Type>,
std::pair<typename std::remove_cvref_t<Type>::first_type,
typename std::remove_cvref_t<Type>::second_type >>;
template<typename T1, typename T2>
struct MyPair
{
typedef T1 first_type; ///< The type of the `first` member
typedef T2 second_type; ///< The type of the `second` member
T1 first; ///< The first member
T2 second; ///< The second member
};
int main()
{
std::pair<int, double> p(1, 2.5);
std::cout << std::boolalpha << pair<decltype(p)> << std::endl; // cout: true
std::pair<int, double> & pref = p ;
std::cout << std::boolalpha << pair<decltype(pref)> << std::endl; // cout: true
const std::pair<int, double> & pref_const = p ;
std::cout << std::boolalpha << pair<decltype(pref_const)> << std::endl; // cout: true
const std::pair<int, double> p_const = p ;
std::cout << std::boolalpha << pair<decltype(p_const)> << std::endl; // cout: true
MyPair<int, double> mp{ 1, 2.5 };
std::cout << std::boolalpha << pair<decltype(mp)> << std::endl; // cout: false
return 0;
}
I dont know too much about how the standard std::pair work , so please comment , ps : If we dont use remove_cvref_t , only By the way , I am inspired by gcc-12 stl in c++/12/bits/stl_pair.h . template<typename _Tp>
inline constexpr bool __is_pair = false;
template<typename _Tp, typename _Up>
inline constexpr bool __is_pair<pair<_Tp, _Up>> = true;
template<typename _Tp, typename _Up>
inline constexpr bool __is_pair<const pair<_Tp, _Up>> = true;
|
|
You are right. We should only support std::pair because struct_pack will try to memcpy it when the first_type & seconde_type are all trivial copyable. It may not work in third-party pair-like type. |
check signature if you want to check the type of parameter really. template<typename T>
concept size_t_param = requires(T obj) {
obj.resize(std::size_t{});
{ &T::resize } -> std::same_as<void(T::*)(std::size_t)>;
}; |
//变长int整数类型
template <typename T>
class varint;
template <typename T>
class sint;
template <typename T>
concept varintable_t =
std::is_same_v<T, varint<int32_t>> || std::is_same_v<T, varint<int64_t>> ||
std::is_same_v<T, varint<uint32_t>> || std::is_same_v<T, varint<uint64_t>>;
template <typename T>
concept sintable_t =
std::is_same_v<T, sint<int32_t>> || std::is_same_v<T, sint<int64_t>>;
template <typename T>
concept varint_t = varintable_t<T> || sintable_t<T>; std::is_same_v<T, varint<int32_t>> || std::is_same_v<T, varint<int64_t>> ||
std::is_same_v<T, varint<uint32_t>> || std::is_same_v<T, varint<uint64_t>>; varint in yalanting meas unsigned var int .But the concept has some mistake. |
simplify: template <class T, class... As>
concept one_of = (std::is_same_v<T, As> || ...);
template <class T>
concept varint = one_of<T, int32_t, int64_t, uint32_t, uint64_t>; |
What happened + What you expected to happen
After reading struct_pack source code , I think threre some code need to be changed , to help readers and developers understande it better .
Reproduction way
I will list what I think below
Anything else
developers can read it and discuss together.
I have many question and suggestion for struct_pack, this issue will not close by me before i write down all of them
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: