尝试实施下面的代码 https://godbolt.org/z/zPMae4:
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 bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97402:
错误 97402- 依赖的partial-concept-id 的值不能在常量表达式中使用。
EDIT 04-Feb-2022: Bug is fixed in GCC 11.1 https://godbolt.org/z/4TGG9zhEf
使用相同的代码 https://godbolt.org/z/v5aG7o当试图让它在 GCC 中编译时会导致'internal compiler error: Segmentation fault'
,在 Clang 中可以正常编译的代码。
EDIT 04-Feb-2022: Also fixed in GCC 11.1 https://godbolt.org/z/hGqTEvrfd
再次尝试使用代码 https://godbolt.org/z/T13KGY导致std::is_same
评价为false
Clang 将其评估为true
.
EDIT 04-Feb-2022: Also fixed in GCC 11.1 https://godbolt.org/z/rGsjjasnT
实施我们自己的is_same https://godbolt.org/z/1vdGnh也没有帮助。
EDIT 04-Feb-2022: Also fixed in GCC 11.1 https://godbolt.org/z/nr5d3jc5c
但需要注意的是,使用std::same_as
作为概念的一部分,用于参数声明 工作正常 https://godbolt.org/z/KG1xd5.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)