作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
正在尝试实现 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/
我是一名优秀的程序员,十分优秀!