Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: 'std::is_same_v<int, T>' is not usable in a constant expression

Trying to implement the following code:

template <typename R, typename V>
concept SizedRangeOf = 
    std::ranges::sized_range<R> &&
    std::same_as<std::ranges::range_value_t<R>, V>;

template<typename T>
const SizedRangeOf<T> auto getView(std::vector<T>& vec) {
    // helper class
    class vector_view {
        std::vector<T>& vec;
    public:
        vector_view(std::vector<T>& vec): vec(vec) {}
        auto begin() const { return vec.begin(); }
        auto end() const { return vec.end(); }
        std::size_t size() const { return vec.size(); }
    };
    return vector_view { vec };
}

int main() {
    std::vector<int> v = {1, 3, 5};
    auto r = getView(v);
    v.push_back(7);
    for(auto val: r) {
        std::cout << val << ' '; // 1 3 5 7
    }
}

Compiles and works fine in Clang 11.0 but fails in GCC 10.2 with the following error:

the value of 'std::is_same_v<int, T>' is not usable in a constant expression

Is it a GCC bug? Or something wrong with the code?

like image 935
Amir Kirsh Avatar asked Mar 01 '23 19:03

Amir Kirsh


1 Answers

It seems to be a GCC bug:

Bug 97402 - Value of dependent partial-concept-id is not usable in a constant expression.

EDIT 04-Feb-2022: Bug is fixed in GCC 11.1


Playing with the same code while trying to make it compile in GCC leads to 'internal compiler error: Segmentation fault', for a code that compiles fine in Clang.

EDIT 04-Feb-2022: Also fixed in GCC 11.1


Another attempt to play with the code leads to std::is_same evaluating to false where Clang evaluates it to true.

EDIT 04-Feb-2022: Also fixed in GCC 11.1

Implementing our own is_same doesn't help either.

EDIT 04-Feb-2022: Also fixed in GCC 11.1


It is to be noted however that using std::same_as as part of a concept, being used for argument declaration works fine.

like image 177
Amir Kirsh Avatar answered Apr 30 '23 18:04

Amir Kirsh