Search code examples
c++std-span

How to initialize std::span<const T*>? Problems with const-ness


I am trying to initialize a span<const T*> — that is, a list of pointers to const data. However, the rules for const conversion amongst pointers and span<>'s available constructors are thwarting me.

I'm sure there's a way to do it, but I cannot find the right casting/invocation.

Aside: I'm actually not using C++20, but tcb::span, which uses the older P0122R7 set of constructors, with (pointer,size) rather than iterators. But I suspect getting something working on C++20 will get me in the right direction.

The below example demonstrates what I'm trying to do, and some failed attempts at fixing it:

Live link on Godbolt

#include<span>
#include<vector>
#include<iostream>
using std::cout;
using std::endl;

int main() {

    int a = 0;
    int b = 1;
    int c = 2;

    std::vector<int*> pointers = { &a, &b, &c };
    
    // This declaration does not work; no span<> constructor matches it.
    std::span<const int*> cspan(&pointers[0], pointers.size());

    // This declaration also does not work; the cast is fine, but still no span<> constructor matches.
    //std::span<const int*> cspan(static_cast<const int *const*>(&pointers[0]), pointers.size());

    // This declaration works, but then "cspan" cannot be sorted, since
    // the pointers themselves are const, and cannot be overwritten.
    //std::span<const int* const> cspan(&pointers[0], pointers.size());
    
    // Sort the span (by address)
    // This is the code I want to work. I just need some way to declare 'cspan' properly.
    std::sort(cspan.begin(), cspan.end());
    
    return 0;
}

Any ideas?


Solution

  • What's wrong with:

    #include<span>
    #include<vector>
    #include<iostream>
    using std::cout;
    using std::endl;
    
    int main() {
    
        int a = 0;
        int b = 1;
        int c = 2;
    
        std::vector<int*> pointers = { &a, &b, &c };
    
        std::span<const int*> cspan(const_cast<const int**>(pointers.data()), pointers.size());
    
        std::sort(cspan.begin(), cspan.end());
        
        return 0;
    }