GCC:'std::is_same_v<int, T>' 在常量表达式中不可用
GCC: 'std::is_same_v<int, T>' is not usable in a constant expression
正在尝试实施 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 的值在常量表达式中不可用。
编辑 2022 年 2 月 4 日: Bug is fixed in GCC 11.1
Playing with the same code 试图使其在 GCC 中编译时导致 'internal compiler error: Segmentation fault'
,对于在 Clang 中编译良好的代码。
编辑 2022 年 2 月 4 日: Also fixed in GCC 11.1
Another attempt to play with the code 导致 std::is_same
评估为 false
,其中 Clang 将其评估为 true
.
编辑 2022 年 2 月 4 日: Also fixed in GCC 11.1
Implementing our own is_same
也无济于事。
编辑 2022 年 2 月 4 日: Also fixed in GCC 11.1
但是需要注意的是,使用 std::same_as
作为概念的一部分,用于参数声明 works fine.
正在尝试实施 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 的值在常量表达式中不可用。
编辑 2022 年 2 月 4 日: Bug is fixed in GCC 11.1
Playing with the same code 试图使其在 GCC 中编译时导致 'internal compiler error: Segmentation fault'
,对于在 Clang 中编译良好的代码。
编辑 2022 年 2 月 4 日: Also fixed in GCC 11.1
Another attempt to play with the code 导致 std::is_same
评估为 false
,其中 Clang 将其评估为 true
.
编辑 2022 年 2 月 4 日: Also fixed in GCC 11.1
Implementing our own is_same
也无济于事。
编辑 2022 年 2 月 4 日: Also fixed in GCC 11.1
但是需要注意的是,使用 std::same_as
作为概念的一部分,用于参数声明 works fine.