[我在C ++ 20中提供的Concepts上进行了一些尝试,并提出了一个简单的示例,令我惊讶的是,does not produce the expected results(请对我的示例的有用性进行任何讨论是:-)):
#include <iostream>
#include <type_traits>
#include <vector>
template <typename T>
concept i_am_integral = requires { std::is_integral_v<T> == true; };
template <typename T> requires i_am_integral<T>
using intvector = std::vector<T>;
int main() {
intvector<int> v = { 1, 2, 3 }; // <- This is fine, int is an integral type
// I would expect this to fail:
intvector<double> w = { 1.1, 2.2, 3.3 };
// I'm curious, so let's print the vector
for (auto x : w) { std::cout << x << '\n'; }
// Same here - IMO, this should fail:
struct S{};
intvector<S> s;
}
我试图将intvector
设置为“受限” std::vector
,仅允许采用整数类型。但是,intvector
似乎吞没了任意类型,就像原始向量一样,包括用户定义的类型。
这是我的错吗?还是lang声不够稳定,无法正确处理此案?我怀疑混合类型别名和需求时存在问题(如this answer中所述),但我无法理解它-特别是我的示例可以编译,而引用的文章中没有。
您的概念: