C++ Logo

std-proposals

Advanced search

Re: A Generic Library for Compile-time Routing

From: Mingxin Wang <mingxwa_at_[hidden]>
Date: Wed, 8 May 2019 15:29:17 +0000
Thank you for the comments. I think you are right since `if constexpr` works even if expressions in unreachable branch are ill-formed.

Thanks,
Mingxin Wang

-----Original Message-----
From: Nicolas Lesser <blitzrakete_at_gmail.com>
Sent: Wednesday, May 8, 2019 9:46 PM
To: Mingxin Wang via Std-Proposals <std-proposals_at_lists.isocpp.org>; std-proposals_at_lists.isocpp.org
Cc: Mingxin Wang <mingxwa_at_microsoft.com>
Subject: RE: [std-proposals] A Generic Library for Compile-time Routing

Mingxin Wang via Std-Proposals wrote:
> Hi folks,
>
> I would like to propose a generic library for compile-time routing.
>
> `if constexpr` and "SFINAE/Concepts based class/function template specialization" are generally used for compile-time routing in complex template libraries. However, according to my experience, they are not so easy to code, maintain or test. Therefore, I designed a template library specifically for compile-time routing with more usability, enabling template library based on this library to have more extendibility and testability.
>
> Please find the draft of the proposal here:
> https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fraw.
> githubusercontent.com%2Fwmx16835%2Fmy-stl%2F4889927d02056acd29f7f98afd
> bb87423878e970%2Fdoc%2Fapplicable_template_preview.pdf&amp;data=01%7C0
> 1%7Cmingxwa%40microsoft.com%7C40c8793088024111cc0808d6d3bb7452%7C72f98
> 8bf86f141af91ab2d7cd011db47%7C1&amp;sdata=K9JjYcYeEDAHELk5LxaOUMAZ6%2F
> Bjy6VGyv6%2FATjNqGA%3D&amp;reserved=0
>
> I am looking forward to your valuable comments.
>
> Thanks,
> Mingxin Wang

Some comments:

> Although if constexpr works at compile-time, this code is incorrect because it never compiles. For example, if std::to_string is applicable to a value of const T& but const T& is not convertible to std::string, there will be always be a compile error in the second if constexpr body.

No that's not true, since this exact problem is why if constexpr exists in the first place! There wouldn't be any difference to a normal if what you're saying were true.
Demo: https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgodbolt.org%2Fz%2FiSKSu0&amp;data=01%7C01%7Cmingxwa%40microsoft.com%7C40c8793088024111cc0808d6d3bb7452%7C72f988bf86f141af91ab2d7cd011db47%7C1&amp;sdata=cw%2FrsK0vRlOMuvMXIBulw5deRmHzwbfl1ujatyHanSA%3D&amp;reserved=0

> Therefore, if we want to solve this problem with function
> template overloads, it is required to use the Concepts and
> carefully design the constraints so that to avoid ambiguation

Yes that's true, but it's not *that* bad. You only need to negate other constraints if there is possible overlapping. For example, !is_primitive_v is not needed for is_container_v, nor is it needed for is_tuple_v and is_string_convertible.

You could even side step this issue by making a concept that has both and using that for an overload (e.g) for types that are a container and are convertible to string and then in it choosing the overload you want to call. Granted, this needs a concept + an overload for each combination, but is otherwise pretty clean. (IMO if constexpr is still the best way for this though).

template <typename T>
concept BothContainerStr = is_string_convertible<T> && is_container_v<T>;

template <typename T> requires BothContainerStr<T> std::string my_to_string_impl(const T& value) { return my_to_string_impl_for_convert_to_string(value); } // use my_to_string_impl_for_convert_to_string for both the above and the 'requires is_string_convertible' case.

> For example, if we want to make a default option rather than producing a compile error if all of the rules above does not apply, and return the string "<unknown>", we will need to write another overload set of my_to_string_impl contains ALL of the basic constraints:

Nope, you just need:

std::string my_to_string_impl(...) { /*...*/ }

since ... has the lowest priority in overload resolution.


Basically, we already have "Another Possible Solution in the Future" right now with C++17. The if constexpr way is wayy cleaner than the metaprogramming way that you propose IMO.

--
- Nicolas

Received on 2019-05-08 10:31:02