Skip to content
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

Extend tuple features (e.g. join) to tuple-like objects #4226

Closed
correaa opened this issue Nov 2, 2024 · 12 comments
Closed

Extend tuple features (e.g. join) to tuple-like objects #4226

correaa opened this issue Nov 2, 2024 · 12 comments

Comments

@correaa
Copy link

correaa commented Nov 2, 2024

For some technical reasons I had to replicate the functionality of std::tuple in my library. (see here: https://github.com/correaa/boost-multi?tab=readme-ov-file#formatting-fmt-pretty-printing)

I would be nice that the features in fmt would work on any type that has std::get<N>, std::tuple_size, etc. in their interface.
That is, if a type is tuple like then it could be printed as a tuple.

(This will also work on any class for which structured binding is implemented).

int main() {

    std::tuple<int, int> t1(1, 2);
    fmt::print( "{}", fmt::join(t1, "x") );  // prints 1 x 2

    my_tuple<int, int> t2(1, 2);
    fmt::print( "{}", fmt::join(t2, "x") );  // doesn't work now
}

Here it is a godbolt example:

https://godbolt.org/z/Gof6f4oaE

@vitaut
Copy link
Contributor

vitaut commented Nov 4, 2024

Thanks for the suggestion. I think it's reasonable to add tuple-like object support to fmt::join and a PR would be welcome.

@correaa
Copy link
Author

correaa commented Nov 11, 2024

Does it work? I think godbolt has updated, but the library still fails for a tuple-like structure:

https://godbolt.org/z/EW4659Yzv

@phprus
Copy link
Contributor

phprus commented Nov 11, 2024

boost::multi has a standard incompatible std::tuple_size implementation.

All specializations of std::tuple_size satisfy UnaryTypeTrait with base characteristic std::integral_constant<std::size_t, N> for some N.

https://en.cppreference.com/w/cpp/utility/tuple_size

But https://gitlab.com/correaa/boost-multi/-/blob/master/include/boost/multi/detail/tuple_zip.hpp?ref_type=heads#L300 :

template<class... Ts>
struct std::tuple_size<boost::multi::detail::tuple<Ts...>> {  // NOLINT(cert-dcl58-cpp) to have structured bindings
	// cppcheck-suppress unusedStructMember
	static constexpr std::size_t value = sizeof...(Ts);
};

does not inherit std::integral_constant.

@correaa
Copy link
Author

correaa commented Nov 11, 2024

Good point, I didn't know that the type has to specifically inherit from std::integral_type, I thought it was enough to have a value static member. Inherithing from std::integral_type seems like an implementation detail.

@phprus
Copy link
Contributor

phprus commented Nov 11, 2024

https://gitlab.com/correaa/boost-multi/-/blob/master/include/boost/multi/detail/tuple_zip.hpp?ref_type=heads#L328

Adding a get function specification into namespace std is undefined behavior.
Use ADL or typle-like.get<i>() for structured binding.

@correaa
Copy link
Author

correaa commented Nov 11, 2024

Yes, the whole reimplementation of tuple was a hack to make the code work on CUDA.
And I had to throw a lot at it to make it work.

In perspective, maybe this is because of the problem you noticed, about inheriting from integral_constant.

@correaa
Copy link
Author

correaa commented Nov 12, 2024

@phprus I think getting into the namespace std was there to be able to access elements using the full syntax std::get.

  1. I think that is the only way to get elements in generic code std::get. If not what I am supposed to do? this? using std::get; get<N>(tp);?

  2. isn't one able to add specialization of some std functions?, I though std::get was one of them.

EDIT: regarding 1), I now remember the problem, in C++17, when I tried to use the ADL get<N> function, I was getting the error "use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Wc++20-extensions]".
The only workaround I found was to really declare, in addition to ADL and member, a proper std::get.
Now I think this was an overkill, the other workaround in C++17 is to put a spurious using std::get; before get so the compiler has at least one candidate to work with.
This is still not perfect because it doesn't work on function signatures (e.g. -> decltype(get<0>(arg)); but it might be preferable to adding this ilegal std specializations.

@phprus
Copy link
Contributor

phprus commented Nov 12, 2024

It is undefined behavior to declare a full specialization of any standard library function template. (since C++20)

https://en.cppreference.com/w/cpp/language/extending_std

@correaa
Copy link
Author

correaa commented Nov 12, 2024

Yes, I agree.
Multi is a C++17 library and this C++20 extension mentioned in the warning comes just in time to not need the std::get specialization.

I now modified the code to not need the specialization std::get (the ADL and members were in place already).

Tomorrow I will try again with fmt to see if it works.

@correaa
Copy link
Author

correaa commented Nov 15, 2024

It works now! https://godbolt.org/z/Mdo8YYqnf

    fmt::print("{} ", fmt::join(arr.sizes(), "×"));  // prints 2×2

    fmt::print("{}\n", fmt::join(arr, "\n"));
    \\ prints
    \\ [3, 4]
    \\ [6, 7]

@correaa
Copy link
Author

correaa commented Nov 15, 2024

A 2D array can be seen as a container of container (range of ranges), so I wonder if one can control the formatting of the nested ranges.

Since this is close to supporting formatting "tables" (but not exactly), I wonder if controlling the format of nested ranges is within the scope of the library: https://stackoverflow.com/questions/79193937/control-fmt-formatting-of-nested-containers-ranges

@vitaut
Copy link
Contributor

vitaut commented Nov 19, 2024

I wonder if controlling the format of nested ranges is within the scope of the library

Discussed in #4240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants