Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if parameter pack contains a std::span<U>

I am iterating over an argument sequence at compile-time, and I want to check if one of the arguments may be a span. If so, I will do span-things.

    template<typename... Args, std::size_t... Indices>
    inline auto SomeClass::resolve_args(std::index_sequence<Indices...>) const
    {
        std::tuple<std::decay_t<Args>...> retval;
        size_t i = 0;
        size_t f = 0;
        ([&] {
            if constexpr (std::is_integral_v<Args>) {
                std::get<Indices>(retval) = sysarg<Args>(i++);
            }
            else if constexpr (std::is_floating_point_v<Args>)
                std::get<Indices>(retval) = sysarg<Args>(f++);
            else if constexpr (std::is_same_v<Args, std::basic_string_view<char>>) {
                std::get<Indices>(retval) = sysarg<Args>(i); i+= 2;
            }
            else if constexpr (is_stdstring<Args>::value)
                std::get<Indices>(retval) = sysarg<Args>(i++);
            else if constexpr (std::is_standard_layout_v<remove_cvref<Args>> && std::is_trivial_v<remove_cvref<Args>>)
                std::get<Indices>(retval) = sysarg<Args>(i++);
            else
                static_assert(always_false<Args>, "Unknown type");
        }(), ...);
        return retval;
    }

Somewhere in there I need to check if Args is a Span. Is that possible?
My problem so far is that my attempts at an if constexpr branch makes other type-combinations fail. So, I am posting the whole function (with minor modifications). You can implement sysarg as a do-nothing function.

Ideally, the spans subtype would be trivial and standard_layout, but I can handle the details. I just need to figure out how to proceed!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Similar to @MSalters answer:

#include <span>
#include <type_traits>

template <typename T>
struct is_span : std::false_type{};
template <typename T>
struct is_span<std::span<T>> : std::true_type{};
template <typename T>
constexpr bool is_span_v = is_span<T>::value;

int main() {
    static_assert(is_span_v<std::span<int>>);
    static_assert(!is_span_v<int>);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading