c++ - 海湾合作委员会 : 'std::is_same_v<int, T>' is not usable in a constant expression

标签 c++ c++20 typetraits c++-concepts

正在尝试实现 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
    }
}
在 Clang 11.0 中编译并正常工作,但在 GCC 10.2 中失败并出现以下错误:
the value of 'std::is_same_v<int, T>' is not usable in a constant expression
这是 GCC 错误吗?还是代码有问题?

最佳答案

好像是GCC bug :
错误 97402 - 依赖部分概念 ID 的值在常量表达式中不可用。
Playing with the same code试图让它在 GCC 中编译会导致 'internal compiler error: Segmentation fault' , 对于在 Clang 中编译良好的代码。
Another attempt to play with the code导致 std::is_same评估为 false Clang 将其计算为 true .
Implementing our own is_same 也没有帮助。

但是需要注意的是,使用 std::same_as作为概念的一部分,用于参数声明 works fine .

关于c++ - 海湾合作委员会 : 'std::is_same_v<int, T>' is not usable in a constant expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65653983/

相关文章:

c++ - C++ 中的数组包装器类

c++ - vector push_back 的空间复杂度

c++ - 为不同的特征专门化相同的运算符

c++ - 在 C++11 使用声明中是否允许/需要 "typename"?

C++2a 合约编程和编译器

c++ - 将非拥有位容器基于 std::vector<bool> 是一个好主意吗?标准::跨度?

c++ - 在 gcc 和 MSVC/clang 之间使用 C++20 重载模板化相等比较运算符的不同结果

c++ - 如何判断一个类型是否派生自模板类?

c++ - 删除右值,保留左值引用(标准类型特征可用?)

c++ - 具有多个声明符的声明 - 定义?