基于范围的 for 循环是否可以采用类型参数
Can a range-based for loop take a type argument?
据我所知,基于范围的 for 循环只能接受 c 样式数组、具有begin()
和end()
定义成员函数的类型对象,或者可以使用 ADL 找到自由函数begin(Type)
和end(Type)
的Type
类型的对象。
有没有办法让循环采用类型参数,以便编译这样的代码?
class StaticVec{
//shortened implementation
static myIterator begin();
static myIterator end();
};
void foo() {
for(auto elem : StaticVec){
dosomething(elem);
}
}
我想省略在循环中编写StaticVec::values()
的必要性。
作为一般解决方案,您可以定义
template< class Type > struct Static_collection {};
template< class Type >
auto begin( Static_collection<Type> const& )
-> decltype( Type::begin() )
{ return Type::begin(); }
template< class Type >
auto end( Static_collection<Type> const& )
-> decltype( Type::end() )
{ return Type::end(); }
然后你可以写例如
auto main() -> int
{
for( auto elem : Static_collection<Static_vec>() )
{
std::cout << elem << ' ';
}
std::cout << 'n';
}
补遗:
然而,在大多数实际情况下,只需创建一个包含静态begin
和end
成员函数的类实例就足够了,如 Jarod42 和 Matt McNabb 的答案所示(前者在我发布上述内容时已经发布),例如
for( auto const& elem : StaticVec() )
{
// ...
}
如果创建实例可能会产生不良的副作用,现在或将来进行一些维护工作后,请使用通用解决方案。
否则,如果实例创建基本上是免费的,我会这样做。
您仍然可以(如果适用)构造一个虚拟对象:
for (auto&& elem : StaticVec{}) {
// ...
}
这绝对是不可能的。示例中的范围变量是StaticVec
(不是变量),因此它扩展的代码将涉及auto it = StaticVec.begin()
或auto it = begin(StaticVec)
,并且这些表达式都无效,函数调用需要对象而不是类型。
这是有效的,因为静态成员函数仍然可以通过.
表示法调用:
#include <iostream>
using namespace std;
struct StaticVec
{
typedef int *myIterator;
static int x[5];
static myIterator begin() { return x; }
static myIterator end() { return x + 5; }
};
int StaticVec::x[5] = { 10, 20, 30, 40, 50 };
void dosomething(int i)
{
cout << i << endl;
}
int main()
{
for(auto elem : StaticVec())
{
dosomething(elem);
}
}
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 编译器在 const ref 类型参数上使用临时对象时是否应该警告不安全的行为?
- 如何检查类型 T 是否在参数包 Ts 中
- 是否可以使用模板非类型参数使用#if指令?(矢量多合一类)
- 类型别名是否用作函数类型参数的一部分,是函数签名的一部分
- 是否可以从C#调用具有数据集类型参数的C++函数
- 如何测试类型参数是否为模板类型
- 基于范围的 for 循环是否可以采用类型参数
- 空值包扩展是否与类型包或可选类型参数匹配
- 如何测试类型是否是带有非类型参数的模板的专门化
- 是否可以基于模板类型参数的嵌套类型定义来专门化模板定义
- C++11: 如何检查类型是否是"heterogeneous"非类型参数的给定类模板的实例化?
- 是否允许将类模板类型参数定义为相同的名称?