【发布时间】:2013-04-10 14:54:09
【问题描述】:
我有一个以整数类型为模板的 C++ 类,例如,
template<typename int_type>
假设在那个类的某个地方,我想使用sscanf 从文件中读取一些值,例如,
int_type num_rows;
fgets( buffer, BUFSIZE, in_file );
sscanf( buffer, "%d", &num_rows);
格式说明符仅在 int_type 是内在 int 时才能正常工作。
有没有更好的方法来处理一般int_type 的格式说明符?
【问题讨论】:
-
这并不是真正的模板化... sscanf with %d 仅适用于整数,因此模板化类型在这里并不重要。您还需要更改 scanf 参数
-
你可以有一个私有静态成员函数,它返回
char const *,并为你打算支持的每个不同的int_type专门化它,并使用返回值作为sscanf()的格式参数。
标签: c++ templates format-specifiers