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

std::span as a template template argument doesn't compile with clang

I want to use std::span as a template template argument to a function.
gcc seems to accept the following code, but clang rejects.

#include <iostream>
#include <span>
#include <vector>

std::vector v{1,2,3,4,5,6};

template <template <typename> class S>
S<int> GetSpan()
{
    return v;
}

int main()
{
    auto x = GetSpan<std::span>();
    return 0;
}

Can someone please explain why this is the case ?

https://godbolt.org/z/Ks9M5oqKc

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 :

You are missing the second template parameter, the extent:

template <template <typename, std::size_t> class S>
S<int, std::dynamic_extent> GetSpan()
{
    return v;
}

To make it work with a custom span-like type as well as std::span you could possibly make it take a non-type parameter pack std::size_t... or auto....

template <template <typename, std::size_t...> class S>
auto GetSpan()
{
    return S(v);
}
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