gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:05:30 32 4
gpt4 key购买 nike

正在尝试实现 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com